import { ArrowPathIcon } from '@heroicons/react/24/solid'
import { isRouteErrorResponse } from 'react-router'

import { getCategories } from '~/apis/common/get-categories'
import { getNews } from '~/apis/common/get-news'
import { Card } from '~/components/ui/card'
import { NewsPage } from '~/pages/news'

import type { Route } from './+types/_news._index'

export const loader = async ({}: Route.LoaderArgs) => {
  const { data: categoriesData } = await getCategories()

  const spotlightCode = 'spotlight'
  const spotlightCategory = categoriesData.find(
    (category) => category.code === spotlightCode,
  )

  const beritaCode = 'berita'
  const beritaCategory = categoriesData.find(
    (category) => category.code === beritaCode,
  )

  const kajianCode = 'kajian'
  const kajianCategory = categoriesData.find(
    (category) => category.code === kajianCode,
  )

  let { data: spotlightNews } = await getNews({
    categories: [spotlightCode],
  })
  spotlightNews = spotlightNews.filter(
    (news) => new Date(news.live_at) <= new Date(),
  )
  let { data: beritaNews } = await getNews({
    categories: [beritaCode],
  })
  beritaNews = beritaNews.filter((news) => new Date(news.live_at) <= new Date())
  let { data: kajianNews } = await getNews({
    categories: [kajianCode],
  })
  kajianNews = kajianNews.filter((news) => new Date(news.live_at) <= new Date())

  return {
    spotlightCategory,
    spotlightCode,
    beritaCategory,
    beritaCode,
    kajianCategory,
    kajianCode,
    spotlightNews,
    beritaNews,
    kajianNews,
  }
}

export const HydrateFallback = () => {
  return (
    <Card>
      <div className="mt-3 mb-3 grid items-center justify-between border-b border-black pb-3 sm:mb-[30px] sm:pb-[30px]">
        <h2 className="text-2xl font-extrabold text-[#2E2F7C] sm:text-4xl">
          Loading...
        </h2>
        <p className="text-xl font-light text-[#777777] italic sm:text-2xl">
          Please wait while we load the content.
        </p>
      </div>
      <div className="flex items-center justify-center p-10">
        <ArrowPathIcon className="size-10 animate-spin text-[#777777]" />
      </div>
    </Card>
  )
}

export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
  let message = 'Oops!'
  let details = 'An unexpected error occurred.'
  let stack: string | undefined

  if (isRouteErrorResponse(error)) {
    message = error.status === 404 ? '404' : 'Error'
    details =
      error.status === 404
        ? 'The requested page could not be found.'
        : error.statusText || details
  } else if (import.meta.env.DEV && error && error instanceof Error) {
    details = error.message
    stack = error.stack
  }

  return (
    <Card>
      <div className="mt-3 mb-3 grid items-center justify-between border-b border-black pb-3 sm:mb-[30px] sm:pb-[30px]">
        <h2 className="text-2xl font-extrabold text-[#2E2F7C] sm:text-4xl">
          {message}
        </h2>
        <p className="text-xl font-light text-[#777777] italic sm:text-2xl">
          {details}
        </p>
      </div>
      {stack && (
        <pre className="w-full whitespace-pre-wrap">
          <code>{stack}</code>
        </pre>
      )}
    </Card>
  )
}

const NewsIndexLayout = () => <NewsPage />

export default NewsIndexLayout
