"use client"

import { Link } from "@/i18n/navigation"
import type { ColumnDef } from "@tanstack/react-table"
import { PencilIcon, Trash2Icon } from "lucide-react"
import * as React from "react"
import { useTranslations } from "next-intl"

import { PageHeader } from "@/components/page-header"
import { SuperDataTable } from "@/components/super-data-table"
import { Button } from "@/components/ui/button"

type TemplateRow = {
  id: string
  code: string
  name: string
}

const data: TemplateRow[] = [
  { id: "1", code: "homepage", name: "Homepage template" },
  { id: "2", code: "footer-links", name: "Footer links" },
]

export default function Page() {
  const t = useTranslations("Cms.Templates")
  const tc = useTranslations("Common")

  const columns = React.useMemo<ColumnDef<TemplateRow>[]>(
    () => [
      {
        accessorKey: "name",
        header: t("columns.template"),
        cell: ({ row }) => (
          <Link
            className="font-medium hover:underline"
            href={`/cms-templates/${row.original.id}/edit`}
          >
            {row.original.name}
          </Link>
        ),
      },
      { accessorKey: "code", header: t("columns.code") },
      {
        id: "edit",
        header: tc("actions.edit"),
        cell: ({ row }) => (
          <Button variant="ghost" size="icon" className="size-8" asChild>
            <Link
              href={`/cms-templates/${row.original.id}/edit`}
              aria-label={`${tc("actions.edit")} ${row.original.name}`}
            >
              <PencilIcon className="size-4" />
            </Link>
          </Button>
        ),
      },
      {
        id: "delete",
        header: tc("actions.delete"),
        cell: ({ row }) => (
          <Button
            variant="ghost"
            size="icon"
            className="size-8 text-destructive hover:text-destructive"
            aria-label={`${tc("actions.delete")} ${row.original.name}`}
            onClick={() => {
              if (!window.confirm(tc("confirm.deleteNamed", { name: row.original.name })))
                return
            }}
          >
            <Trash2Icon className="size-4" />
          </Button>
        ),
      },
    ],
    [t, tc]
  )

  return (
    <div className="flex flex-col gap-4 py-4 md:gap-6 md:py-6">
      <PageHeader
        title={t("title")}
        actions={
          <Button asChild>
            <Link href="/cms-templates/create">{t("actions.addTemplate")}</Link>
          </Button>
        }
      />
      <SuperDataTable
        data={data}
        columns={columns}
        searchKey="name"
        searchPlaceholder={t("placeholders.searchTemplates")}
      />
    </div>
  )
}

