import { useState } from "react";
import {
  CheckCircle,
  XCircle,
  Loader2,
  Shield,
  ShieldCheck,
  FileIcon,
  X,
} from "lucide-react";
import type { Test } from "../../types/models";

// ── Props ──────────────────────────────────────────────────────────

interface ValidationModalProps {
  side: "red" | "blue";
  test: Test;
  isSubmitting: boolean;
  onSubmit: (status: "approved" | "rejected", notes: string) => void;
  onClose: () => void;
}

// ── Component ──────────────────────────────────────────────────────

export default function ValidationModal({
  side,
  test,
  isSubmitting,
  onSubmit,
  onClose,
}: ValidationModalProps) {
  const [decision, setDecision] = useState<"approved" | "rejected" | null>(null);
  const [notes, setNotes] = useState("");

  const isRed = side === "red";
  const title = isRed ? "Validate as Red Lead" : "Validate as Blue Lead";
  const accent = isRed ? "orange" : "indigo";

  // Evidence from the corresponding team
  const evidences = isRed ? test.red_evidences || [] : test.blue_evidences || [];

  // Status of the other side's validation
  const otherStatus = isRed
    ? test.blue_validation_status
    : test.red_validation_status;
  const otherLabel = isRed ? "Blue Lead" : "Red Lead";

  // Can submit?
  const canSubmit =
    decision !== null &&
    !isSubmitting &&
    (decision === "approved" || notes.trim().length > 0);

  // ── Render ───────────────────────────────────────────────────────

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm">
      <div className="w-full max-w-lg rounded-xl border border-gray-800 bg-gray-900 shadow-xl">
        {/* Header */}
        <div className="flex items-center justify-between border-b border-gray-800 px-6 py-4">
          <div className="flex items-center gap-2">
            {isRed ? (
              <Shield className={`h-5 w-5 text-${accent}-400`} />
            ) : (
              <ShieldCheck className={`h-5 w-5 text-${accent}-400`} />
            )}
            <h3 className="text-lg font-semibold text-white">{title}</h3>
          </div>
          <button
            onClick={onClose}
            className="rounded p-1 text-gray-400 hover:bg-gray-800 hover:text-white"
          >
            <X className="h-5 w-5" />
          </button>
        </div>

        {/* Body */}
        <div className="space-y-5 px-6 py-5">
          {/* Evidence summary */}
          <div>
            <h4 className="mb-2 text-sm font-medium text-gray-300">
              {isRed ? "Red" : "Blue"} Team Evidence ({evidences.length})
            </h4>
            {evidences.length > 0 ? (
              <div className="max-h-32 space-y-1 overflow-y-auto rounded-lg border border-gray-700 bg-gray-800/50 p-2">
                {evidences.map((ev) => (
                  <div key={ev.id} className="flex items-center gap-2 text-xs text-gray-400">
                    <FileIcon className="h-3.5 w-3.5 text-gray-500" />
                    <span className="truncate">{ev.file_name}</span>
                  </div>
                ))}
              </div>
            ) : (
              <p className="text-xs text-gray-500">No evidence files uploaded.</p>
            )}
          </div>

          {/* Other side status */}
          <div className="rounded-lg border border-gray-700 bg-gray-800/50 p-3">
            <span className="text-xs font-medium text-gray-500">{otherLabel} status: </span>
            {otherStatus === "approved" ? (
              <span className="inline-flex items-center gap-1 text-xs text-green-400">
                <CheckCircle className="h-3.5 w-3.5" /> Approved
              </span>
            ) : otherStatus === "rejected" ? (
              <span className="inline-flex items-center gap-1 text-xs text-red-400">
                <XCircle className="h-3.5 w-3.5" /> Rejected
              </span>
            ) : (
              <span className="text-xs text-gray-400">Pending</span>
            )}
          </div>

          {/* Decision */}
          <div>
            <h4 className="mb-2 text-sm font-medium text-gray-300">Decision</h4>
            <div className="flex gap-3">
              <button
                onClick={() => setDecision("approved")}
                className={`flex flex-1 items-center justify-center gap-2 rounded-lg border p-3 text-sm font-medium transition-colors ${
                  decision === "approved"
                    ? "border-green-500 bg-green-500/10 text-green-400"
                    : "border-gray-700 bg-gray-800 text-gray-400 hover:border-gray-600"
                }`}
              >
                <CheckCircle className="h-4 w-4" />
                Approve
              </button>
              <button
                onClick={() => setDecision("rejected")}
                className={`flex flex-1 items-center justify-center gap-2 rounded-lg border p-3 text-sm font-medium transition-colors ${
                  decision === "rejected"
                    ? "border-red-500 bg-red-500/10 text-red-400"
                    : "border-gray-700 bg-gray-800 text-gray-400 hover:border-gray-600"
                }`}
              >
                <XCircle className="h-4 w-4" />
                Reject
              </button>
            </div>
          </div>

          {/* Notes */}
          <div>
            <label className="mb-1.5 block text-sm font-medium text-gray-300">
              Notes{decision === "rejected" && <span className="text-red-400"> (required)</span>}
            </label>
            <textarea
              value={notes}
              onChange={(e) => setNotes(e.target.value)}
              rows={3}
              className="w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-sm text-gray-200 placeholder-gray-500 focus:border-cyan-500 focus:outline-none focus:ring-1 focus:ring-cyan-500"
              placeholder={
                decision === "rejected"
                  ? "Explain why this is being rejected..."
                  : "Optional notes..."
              }
            />
            {decision === "rejected" && notes.trim().length === 0 && (
              <p className="mt-1 text-xs text-red-400">
                Notes are required when rejecting.
              </p>
            )}
          </div>
        </div>

        {/* Footer */}
        <div className="flex justify-end gap-3 border-t border-gray-800 px-6 py-4">
          <button
            onClick={onClose}
            disabled={isSubmitting}
            className="rounded-lg border border-gray-700 px-4 py-2 text-sm text-gray-400 hover:bg-gray-800 disabled:opacity-50"
          >
            Cancel
          </button>
          <button
            onClick={() => decision && onSubmit(decision, notes)}
            disabled={!canSubmit}
            className={`flex items-center gap-1.5 rounded-lg px-4 py-2 text-sm font-medium text-white transition-colors disabled:opacity-50 ${
              decision === "rejected"
                ? "bg-red-600 hover:bg-red-500"
                : "bg-green-600 hover:bg-green-500"
            }`}
          >
            {isSubmitting && <Loader2 className="h-4 w-4 animate-spin" />}
            {decision === "approved"
              ? "Confirm Approval"
              : decision === "rejected"
                ? "Confirm Rejection"
                : "Select a decision"}
          </button>
        </div>
      </div>
    </div>
  );
}
