"use client"

import { Link } from "@/i18n/navigation"
import type { ColumnDef } from "@tanstack/react-table"
import {useMemo} 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 CurrencyRow = {
  id: string
  code: string
  name: string
  symbol: string
}

const data: CurrencyRow[] = [
  { id: "USD", code: "USD", name: "US Dollar", symbol: "$" },
  { id: "EUR", code: "EUR", name: "Euro", symbol: "€" },
]

export default function Page() {
  const t = useTranslations("Currencies")
  const tc = useTranslations("Common")

  const columns = useMemo<ColumnDef<CurrencyRow>[]>(
    () => [
      { accessorKey: "code", header: t("columns.code") },
      { accessorKey: "name", header: t("columns.name") },
      { accessorKey: "symbol", header: t("columns.symbol") },
    ],
    [t]
  )

  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="/currencies/create">{t("actions.addCurrency")}</Link>
          </Button>
        }
      />
      <SuperDataTable
        data={data}
        columns={columns}
        searchKey="name"
        searchPlaceholder={t("placeholders.searchCurrencies")}
        actionsForRow={(row) => [
          { type: "link", label: tc("actions.edit"), href: () => `/currencies/${row.id}/edit` },
          { type: "separator" },
          {
            type: "action",
            label: tc("actions.delete"),
            destructive: true,
            onClick: () => {
              if (!window.confirm(tc("confirm.deleteNamed", { name: row.name }))) return
            },
          },
        ]}
      />
    </div>
  )
}

