import {
  PencilSquareIcon,
  PlusIcon,
  TrashIcon,
} from '@heroicons/react/24/solid'
import DT, { type Config, type ConfigColumns } from 'datatables.net-dt'
import DataTable, { type DataTableSlots } from 'datatables.net-react'
import { useState } from 'react'
import { Link, useRouteLoaderData } from 'react-router'

import type { TCategoryResponse } from '~/apis/common/get-categories'
import type { TAuthorResponse, TNewsResponse } from '~/apis/common/get-news'
import type { TTagResponse } from '~/apis/common/get-tags'
import { DialogDelete } from '~/components/dialog/delete'
import { Button } from '~/components/ui/button'
import { UiTable } from '~/components/ui/table'
import { TitleDashboard } from '~/components/ui/title-dashboard'
import type { loader } from '~/routes/_admin.lg-admin._dashboard.contents._index'
import { formatDate } from '~/utils/formatter'

export const ContentsPage = () => {
  const loaderData = useRouteLoaderData<typeof loader>(
    'routes/_admin.lg-admin._dashboard.contents._index',
  )
  const [selectedContent, setSelectedContent] = useState<TNewsResponse>()
  DataTable.use(DT)
  const dataTable =
    loaderData?.newsData?.sort(
      (a, b) => new Date(b.live_at).getTime() - new Date(a.live_at).getTime(),
    ) || []
  const dataColumns: ConfigColumns[] = [
    {
      title: 'No',
      render: (
        _data: unknown,
        _type: unknown,
        _row: unknown,
        meta: { row: number },
      ) => {
        return meta.row + 1
      },
    },
    {
      title: 'Tanggal Live',
      data: 'live_at',
    },
    {
      title: 'Penulis',
      data: 'author',
    },
    { title: 'Judul', data: 'title' },
    {
      title: 'Kategori',
      data: 'categories',
    },
    { title: 'Tag', data: 'tags' },
    {
      title: 'Subscription',
      data: 'is_premium',
    },
    {
      title: 'Action',
      data: 'slug',
    },
  ]
  const dataSlot: DataTableSlots = {
    1: (value: string) => formatDate(value),
    2: (value: TAuthorResponse) => (
      <div>
        <div>{value.name}</div>
        <div className="text-sm text-[#7C7C7C]">ID: {value.id.slice(0, 8)}</div>
      </div>
    ),
    3: (value: string) => <span className="text-sm">{value}</span>,
    4: (value: TCategoryResponse[]) => (
      <div className="text-xs">{value.map((item) => item.name).join(', ')}</div>
    ),
    5: (value: TTagResponse[]) => (
      <div className="text-xs">{value.map((item) => item.name).join(', ')}</div>
    ),
    6: (value: string) =>
      value ? (
        <div className="rounded-full bg-[#FFFCAF] px-2 text-center text-[#DBCA6E]">
          Premium
        </div>
      ) : (
        <div className="rounded-full bg-[#F5F5F5] px-2 text-center text-[#4C5CA0]">
          Normal
        </div>
      ),
    7: (value: string, _type: unknown, data: TNewsResponse) => (
      <div className="flex space-x-2">
        <Button
          as="a"
          href={`/lg-admin/contents/update/${encodeURIComponent(value)}`}
          size="icon"
          title="Update Artikel"
        >
          <PencilSquareIcon className="size-4" />
        </Button>
        <Button
          type="button"
          size="icon"
          variant="danger"
          onClick={() => setSelectedContent(data)}
          title="Hapus Artikel"
        >
          <TrashIcon className="size-4" />
        </Button>
      </div>
    ),
  }
  const dataOptions: Config = {
    paging: true,
    searching: true,
    ordering: true,
    info: true,
  }

  return (
    <div className="relative">
      <TitleDashboard title="Artikel" />
      <div className="mb-8 flex items-end justify-between gap-5">
        <div className="flex-1">{/* TODO: Filter */}</div>
        <Button
          as={Link}
          to="/lg-admin/contents/create"
          size="lg"
          className="text-md h-[42px] px-4"
        >
          <PlusIcon className="size-8" /> Buat Artikel
        </Button>
      </div>

      <UiTable
        data={dataTable}
        columns={dataColumns}
        slots={dataSlot}
        options={dataOptions}
        title="Daftar Artikel"
      />

      <DialogDelete
        selectedId={selectedContent?.id}
        close={() => setSelectedContent(undefined)}
        title="Artikel"
        fetcherAction={`/actions/admin/contents/delete/${selectedContent?.id}`}
      >
        <p>{selectedContent?.title}</p>
      </DialogDelete>
    </div>
  )
}
