import { notFound } from "next/navigation";

import { Card, CardContent } from "@/components/ui/card";

import { getOrderBlotterDetail } from "../_components/get-detail";
import { getOrderBlotterHistory } from "../_components/get-history";
import { OrderBlotterDetailHeader } from "../_components/order-blotter-detail-header";
import { OrderBlotterHistoryHeader } from "../_components/order-blotter-history-header";
import { OrderBlotterHistoryTimeline } from "../_components/order-blotter-history-timeline";
import { OrderBlotterViewDocument } from "../_components/order-blotter-view-document";
import { OrderBlotterUpdateForm } from "../_components/order-blotter-update-form";

type PageProps = {
  params: Promise<{ id: string }>;
  searchParams: Promise<{ action?: string; ref?: string; from?: string }>;
};

export default async function OrderBlotterDetailPage({ params, searchParams }: PageProps) {
  const { id: idParam } = await params;
  const { action, ref, from } = await searchParams;
  const id = Number(idParam);
  const hasRef = typeof ref === "string" && ref.trim() !== "";
  const fromList = from === "list";

  if (!hasRef && !Number.isFinite(id)) {
    notFound();
  }

  const lookup = {
    id: Number.isFinite(id) && id > 0 ? id : null,
    ref: hasRef ? ref.trim() : null,
  };

  const detail = await getOrderBlotterDetail(lookup);
  if (!detail) {
    notFound();
  }

  const resolvedAction = action ?? "view";
  const resolvedId = detail.id > 0 ? detail.id : id;
  const listPath = "/dashboard/order-blotter";
  const listLabel = fromList ? "Back to order blotter" : "Back to list";

  if (resolvedAction === "history") {
    const history = await getOrderBlotterHistory(lookup);
    if (!history) {
      notFound();
    }

    return (
      <div className="flex flex-col gap-3">
        <OrderBlotterHistoryHeader id={resolvedId} listPath={listPath} listLabel={listLabel} />
        <Card className="gap-0 overflow-hidden border-border/80 py-0 shadow-sm">
          <CardContent className="p-4">
            <OrderBlotterHistoryTimeline history={history} />
          </CardContent>
        </Card>
      </div>
    );
  }

  if (resolvedAction === "view") {
    const history = await getOrderBlotterHistory(lookup);
    return (
      <div className="flex flex-col gap-3">
        <OrderBlotterDetailHeader
          id={resolvedId}
          action="view"
          listPath={listPath}
          listLabel={listLabel}
          refToken={lookup.ref}
          fromList={fromList}
          historyInSheet
          history={history}
        />
        <Card className="gap-0 overflow-hidden border-border/80 py-0 shadow-sm">
          <CardContent className="p-4">
            <OrderBlotterViewDocument detail={detail} />
          </CardContent>
        </Card>
      </div>
    );
  }

  if (resolvedAction === "update") {
    return (
      <div className="flex flex-col gap-4 md:gap-6">
        <OrderBlotterDetailHeader
          id={resolvedId}
          action="update"
          listPath={listPath}
          listLabel={listLabel}
          refToken={lookup.ref}
          fromList={fromList}
          historyInSheet
        />
        <Card>
          <CardContent className="pt-6">
            <OrderBlotterUpdateForm detail={detail} />
          </CardContent>
        </Card>
      </div>
    );
  }

  if (resolvedAction === "export" || resolvedAction === "pdf") {
    return (
      <div className="flex flex-col gap-4 md:gap-6">
        <OrderBlotterDetailHeader id={resolvedId} action={resolvedAction} />
        <Card>
          <CardContent className="py-10 text-center text-muted-foreground text-sm">
            The <span className="font-medium text-foreground">{resolvedAction}</span> screen is not
            implemented yet.{" "}
            <a
              href={`/dashboard/order-blotter/${resolvedId}?action=view${hasRef ? `&ref=${encodeURIComponent(ref!.trim())}` : ""}`}
              className="text-primary underline-offset-4 hover:underline"
            >
              View this register
            </a>
          </CardContent>
        </Card>
      </div>
    );
  }

  notFound();
}
