"use client";

import type { ColumnDef } from "@tanstack/react-table";
import Link from "next/link";

import { DashboardMasterRowActions } from "@/app/dashboard/_components/dashboard-master-row-actions";
import { Input } from "@/components/ui/input";

import { ActiveInactiveBadge } from "@/components/shared/status-pill";
import { YesNoBadge } from "./status-badge";
import type { CurrencyRow } from "./schema";

type PriorityHandlers = {
  priorities: Record<number, number>;
  onPriorityChange: (id: number, priority: number) => void;
};

const DELETE_HREF = "/dashboard/master/currencies/delete";

function CurrencyActions({ row }: { row: CurrencyRow }) {
  const base = `/dashboard/master/currencies/${row.id}`;

  return (
    <DashboardMasterRowActions
      editHref={base}
      editAriaLabel="Update currency"
      deleteUrl={DELETE_HREF}
      entityId={row.id}
      entityName={row.name}
      entityLabel="currency"
      fallbackError="Currency could not be deleted."
      defaultSuccessMessage="Currency deleted."
      showDelete={row.isRemovable}
    />
  );
}

export function createCurrencyColumns({
  priorities,
  onPriorityChange,
}: PriorityHandlers): ColumnDef<CurrencyRow>[] {
  return [
    {
      accessorKey: "name",
      header: "Name",
      cell: ({ row }) => (
        <Link
          href={`/dashboard/master/currencies/${row.original.id}`}
          className="font-medium text-primary hover:underline"
        >
          {row.original.name}
        </Link>
      ),
    },
    {
      accessorKey: "code",
      header: "Code",
      cell: ({ row }) => (
        <span className="inline-flex rounded-md border bg-muted/50 px-2 py-0.5 font-mono font-semibold text-xs uppercase tracking-wide">
          {row.original.code}
        </span>
      ),
    },
    {
      accessorKey: "isDefault",
      header: "Is default",
      cell: ({ row }) => (
        <YesNoBadge value={row.original.isDefault} label={row.original.isDefaultLabel} />
      ),
    },
    {
      accessorKey: "status",
      header: "Status",
      cell: ({ row }) => (
        <ActiveInactiveBadge
          status={row.original.status}
          label={row.original.statusLabel}
        />
      ),
    },
    {
      accessorKey: "dateAdded",
      header: "Date added",
      enableColumnFilter: false,
      cell: ({ row }) => (
        <span className="text-muted-foreground text-sm whitespace-nowrap">
          {row.original.dateAdded}
        </span>
      ),
    },
    {
      id: "priority",
      header: "Priority",
      enableColumnFilter: false,
      cell: ({ row }) => {
        const id = row.original.id;
        const value = priorities[id] ?? row.original.priority;

        return (
          <Input
            type="number"
            min={0}
            className="h-8 w-14 bg-background text-center text-xs tabular-nums"
            value={value}
            onChange={(event) => {
              const parsed = Number(event.target.value);
              onPriorityChange(id, Number.isFinite(parsed) ? parsed : 0);
            }}
          />
        );
      },
    },
    {
      id: "options",
      header: () => <span className="block w-full text-right">Options</span>,
      cell: ({ row }) => <CurrencyActions row={row.original} />,
      enableSorting: false,
    },
  ];
}
