"use client";

import Link from "next/link";
import { CalendarDays } from "lucide-react";

import { TransactionDetailSheet } from "@/components/customer/transaction-detail-sheet";
import { Button } from "@/components/ui/button";

import {
  isStockAccumulatorTransaction,
  stockAccumulatorScheduleHref,
} from "../../_lib/stock-accumulator-nav";
import type { TransactionRow } from "./schema";

const DETAIL_CONFIG = {
  entityLabel: "Transaction",
  showActions: false,
} as const;

export function AllTransactionsDetailSheet({
  row,
  open,
  onOpenChange,
  tenant,
}: {
  row: TransactionRow | null;
  open: boolean;
  onOpenChange: (open: boolean) => void;
  tenant: string;
}) {
  if (!row) return null;

  const accumulatorHref =
    isStockAccumulatorTransaction(row) && tenant
      ? stockAccumulatorScheduleHref(row, tenant)
      : null;

  return (
    <TransactionDetailSheet
      row={{
        id: row.id,
        refId: row.refId,
        placementDate: row.placementDate,
        bank: row.bank,
        ticker: row.securityTicker,
        name: row.securityName,
        type: row.category,
        executionType: "—",
        transaction: "—",
        price: row.price,
        quantity: row.quantity,
        currency: row.currency,
        amount: row.amount,
      }}
      open={open}
      onOpenChange={onOpenChange}
      config={DETAIL_CONFIG}
      footer={
        accumulatorHref ? (
          <Button asChild className="h-11 w-full gap-2 rounded-xl shadow-sm">
            <Link href={accumulatorHref}>
              <CalendarDays className="size-4" />
              Open stock accumulator schedule
            </Link>
          </Button>
        ) : undefined
      }
    />
  );
}
