"use client";

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

import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";

import { buildFixHref } from "./constants";
import type { ExcludedFromTotalValueRow } from "./schema";

function reasonVariant(
  code: ExcludedFromTotalValueRow["reasonCode"],
): "destructive" | "secondary" | "outline" {
  if (code === "missing_market_price") return "destructive";
  if (code === "missing_fx") return "secondary";
  return "outline";
}

export function createExcludedFromTotalValueColumns(
  pageOffset: number,
): ColumnDef<ExcludedFromTotalValueRow>[] {
  return [
    {
      id: "rowNumber",
      header: "#",
      enableColumnFilter: false,
      cell: ({ row }) => (
        <span className="text-muted-foreground text-sm tabular-nums">
          {pageOffset + row.index + 1}
        </span>
      ),
    },
    {
      accessorKey: "isin",
      header: "ISIN",
      cell: ({ row }) => (
        <span className="font-mono text-sm">{row.original.isin || "—"}</span>
      ),
    },
    {
      accessorKey: "name",
      header: "Name",
      cell: ({ row }) => (
        <span className="text-sm">{row.original.name || "—"}</span>
      ),
    },
    {
      accessorKey: "bankName",
      header: "Bank",
      cell: ({ row }) => (
        <span className="text-sm">{row.original.bankName || "—"}</span>
      ),
    },
    {
      accessorKey: "fTypeLabel",
      header: "Type",
      cell: ({ row }) => (
        <span className="text-sm">
          {row.original.fTypeLabel}
          {row.original.assetTypeTitle
            ? ` · ${row.original.assetTypeTitle}`
            : ""}
        </span>
      ),
    },
    {
      id: "currencies",
      header: "Currency",
      enableColumnFilter: false,
      cell: ({ row }) => {
        const { currency, reportingCurrency } = row.original;
        if (!currency && !reportingCurrency) return "—";
        if (currency && reportingCurrency && currency !== reportingCurrency) {
          return (
            <span className="text-sm tabular-nums">
              {currency} → {reportingCurrency}
            </span>
          );
        }
        return (
          <span className="text-sm tabular-nums">
            {currency || reportingCurrency}
          </span>
        );
      },
    },
    {
      accessorKey: "marketPrice",
      header: "Market price",
      cell: ({ row }) => (
        <span className="text-sm tabular-nums">
          {row.original.marketPrice ?? "—"}
        </span>
      ),
    },
    {
      accessorKey: "fxRate",
      header: "FX rate",
      cell: ({ row }) => (
        <span className="text-sm tabular-nums">
          {row.original.fxRate ?? "—"}
        </span>
      ),
    },
    {
      accessorKey: "reasonLabel",
      header: "Reason",
      cell: ({ row }) => (
        <Badge variant={reasonVariant(row.original.reasonCode)}>
          {row.original.reasonLabel}
        </Badge>
      ),
    },
    {
      id: "option",
      header: "Fix",
      enableColumnFilter: false,
      cell: ({ row }) => {
        const href = buildFixHref(row.original);
        if (!href) return null;

        const label =
          row.original.reasonCode === "missing_fx"
            ? "Add FX rate"
            : "Add price";

        return (
          <Button variant="link" size="sm" className="h-auto px-0" asChild>
            <Link href={href}>{label}</Link>
          </Button>
        );
      },
    },
  ];
}
