"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 ExchangeRateRow = {
  id: string
  base: string
  target: string
  rate: number
}

const data: ExchangeRateRow[] = [
  { id: "USD-EUR", base: "USD", target: "EUR", rate: 0.92 },
  { id: "USD-GBP", base: "USD", target: "GBP", rate: 0.79 },
]

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

  const columns = useMemo<ColumnDef<ExchangeRateRow>[]>(
    () => [
      { accessorKey: "base", header: t("columns.base") },
      { accessorKey: "target", header: t("columns.target") },
      { accessorKey: "rate", header: t("columns.rate") },
    ],
    [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="/exchange-rates/create">{t("actions.addExchangeRate")}</Link>
          </Button>
        }
      />
      <SuperDataTable
        data={data}
        columns={columns}
        searchKey="base"
        searchPlaceholder={t("placeholders.searchExchangeRates")}
        actionsForRow={(row) => [
          { type: "link", label: tc("actions.edit"), href: () => `/exchange-rates/${row.id}/edit` },
          { type: "separator" },
          {
            type: "action",
            label: tc("actions.delete"),
            destructive: true,
            onClick: () => {
              if (!window.confirm(tc("confirm.deleteNamed", { name: row.id }))) return
            },
          },
        ]}
      />
    </div>
  )
}

