import { useState } from "react";
import { Button, Group, Modal, Stack, Text } from "@mantine/core";
import { DateInput } from "@mantine/dates";
import { Ship, Upload } from "lucide-react";
import toast from "react-hot-toast";

import { PhasedFileDropzone } from "@/components/contracts/PhasedFileDropzone";
import { findWorkflowFile } from "@/components/contracts/PhasedUploadedFileRow";
import { contractsService } from "@/services/contracts.service";
import { bookingsService } from "@/services/bookings.service";
import type { Freight } from "@edr/types";

export type GlClearanceUploadKind = "do" | "ro";

export interface GlClearanceUploadModalProps {
  opened: boolean;
  kind: GlClearanceUploadKind | null;
  onClose: () => void;
  entityId: string;
  isBooking: boolean;
  workflowFiles?: Freight.ClearanceWorkflowFile[];
  vesselDepartureDate?: string | null;
  onSuccess?: () => void;
  onPreview?: (file: { name: string; url: string }) => void;
}

export function GlClearanceUploadModal({
  opened,
  kind,
  onClose,
  entityId,
  isBooking,
  workflowFiles = [],
  vesselDepartureDate,
  onSuccess,
  onPreview,
}: GlClearanceUploadModalProps) {
  const [file, setFile] = useState<File | null>(null);
  const [vesselDate, setVesselDate] = useState<Date | null>(
    vesselDepartureDate ? new Date(vesselDepartureDate) : null,
  );
  const [loading, setLoading] = useState(false);

  const isDo = kind === "do";
  const isRo = kind === "ro";
  const replaceMode = isDo
    ? Boolean(findWorkflowFile(workflowFiles, "delivery_order"))
    : Boolean(findWorkflowFile(workflowFiles, "release_order"));

  const close = () => {
    setFile(null);
    onClose();
  };

  const submit = async () => {
    if (!file || !kind) return;
    if (isRo && !vesselDate) {
      toast.error("Vessel departure date is required.");
      return;
    }

    setLoading(true);
    try {
      if (isDo) {
        if (isBooking) {
          await bookingsService.uploadDeliveryOrder(entityId, file);
        } else {
          await contractsService.uploadDeliveryOrder(entityId, file);
        }
        toast.success(replaceMode ? "Delivery Order updated" : "Delivery Order uploaded");
      } else {
        const iso = vesselDate!.toISOString().slice(0, 10);
        const result = isBooking
          ? await bookingsService.uploadReleaseOrder(entityId, file, iso)
          : await contractsService.uploadReleaseOrder(entityId, file, iso);
        if (result.hold) {
          toast.error(result.holdReason ?? "Vessel date too soon");
        } else {
          toast.success(replaceMode ? "Release Order updated" : "Release Order uploaded");
        }
      }
      setFile(null);
      onSuccess?.();
      close();
    } catch (e) {
      toast.error(e instanceof Error ? e.message : "Upload failed");
    } finally {
      setLoading(false);
    }
  };

  return (
    <Modal
      opened={opened && kind != null}
      onClose={close}
      title={
        <Group gap={8}>
          <Ship size={18} />
          <Text fw={700}>{isDo ? "Upload Delivery Order" : "Upload Release Order"}</Text>
        </Group>
      }
      radius="md"
      size="md"
    >
      <Stack gap="md">
        <Text size="sm" c="dimmed">
          {isDo
            ? "Upload the Djibouti Delivery Order (DO) for this import shipment."
            : "Upload the Release Order and confirm the vessel departure date."}
        </Text>

        {isRo ? (
          <DateInput
            label="Vessel departure date"
            value={vesselDate}
            onChange={(v) => setVesselDate(v ? new Date(v) : null)}
            size="sm"
            required
          />
        ) : null}

        <PhasedFileDropzone
          label={isDo ? "Delivery Order file" : "Release Order file"}
          description="PDF or image."
          value={file}
          onChange={setFile}
          replaceMode={replaceMode}
          onPreview={onPreview}
        />

        <Group justify="flex-end" gap="sm">
          <Button variant="default" onClick={close} disabled={loading}>
            Cancel
          </Button>
          <Button
            color="edr-green"
            loading={loading}
            disabled={!file || (isRo && !vesselDate)}
            leftSection={<Upload size={16} />}
            onClick={() => void submit()}
          >
            {replaceMode
              ? isDo
                ? "Replace DO"
                : "Replace RO"
              : isDo
                ? "Upload DO"
                : "Upload RO"}
          </Button>
        </Group>
      </Stack>
    </Modal>
  );
}
