import { useMemo } from "react";
import { Stack, Text } from "@mantine/core";
import type { Freight } from "@edr/types";

import { SectionCard } from "@/components/bookings/detail/SectionCard";
import { Flag } from "lucide-react";

import { AssignStationCard } from "./AssignStationCard";
import { AssignRiskCard } from "./AssignRiskCard";
import { AdviseDutyCard } from "./AdviseDutyCard";
import { GlDocumentUploadCard } from "./GlDocumentUploadCard";
import { TransportDocumentCard } from "./TransportDocumentCard";
import { IncidentReportCard } from "./IncidentReportCard";

export interface GlActionsPanelProps {
  bookingId: string;
  milestones: Freight.IClearanceMilestone[];
}

/** Find a milestone by code (post-booking milestones live on the booking). */
function findMilestone(
  milestones: Freight.IClearanceMilestone[],
  code: string,
): Freight.IClearanceMilestone | undefined {
  return milestones.find((m) => m.milestoneCode === code);
}

/**
 * Global Logistics action surface for a shipment. Each card is gated by whether
 * its milestone exists on this shipment (import vs export differ) and renders the
 * structured action (risk level, duty advice, document upload, incident report,
 * station routing) that the plain "Complete" button can't capture.
 */
export function GlActionsPanel({ bookingId, milestones }: GlActionsPanelProps) {
  const riskMs = useMemo(
    () => findMilestone(milestones, "RISK_ASSIGNED"),
    [milestones],
  );
  const dutyMs = useMemo(
    () => findMilestone(milestones, "DUTY_TAXES_ADVISED"),
    [milestones],
  );
  const wagonMs = useMemo(
    () => findMilestone(milestones, "WAGON_ALLOCATED"),
    [milestones],
  );
  const transportMs = useMemo(
    () => findMilestone(milestones, "EXPORT_TRANSPORT_ISSUED"),
    [milestones],
  );
  const showTransport =
    wagonMs?.status === "COMPLETED" && transportMs?.status !== "COMPLETED";

  return (
    <SectionCard icon={Flag} title="Global Logistics actions" accent="edr-green">
      <Stack gap="md">
        <Text size="xs" c="dimmed">
          Structured GL operations for this shipment. Uploading a document
          advances its milestone automatically.
        </Text>

        <AssignStationCard bookingId={bookingId} />

        {dutyMs ? (
          <AdviseDutyCard bookingId={bookingId} milestone={dutyMs} />
        ) : null}

        <GlDocumentUploadCard bookingId={bookingId} />

        {showTransport ? <TransportDocumentCard bookingId={bookingId} /> : null}

        {riskMs ? (
          <AssignRiskCard
            bookingId={bookingId}
            milestone={riskMs}
            locked={dutyMs?.status === "COMPLETED"}
          />
        ) : null}

        <IncidentReportCard bookingId={bookingId} />
      </Stack>
    </SectionCard>
  );
}
