"use client";

import React from "react";
import { X, ExternalLink, CheckCircle2, AlertTriangle, XCircle, Clock } from "lucide-react";

interface ScreenshotModalProps {
  isOpen: boolean;
  onClose: () => void;
  job: {
    id: number;
    patientName: string;
    mobile: string;
    priorityOrder: number;
    targetTime: string;
    status: string;
    resultSerial?: string | null;
    confirmationNo?: string | null;
    errorMessage?: string | null;
    screenshotPath?: string | null;
    appointmentDate: string;
  } | null;
}

export const ScreenshotModal: React.FC<ScreenshotModalProps> = ({ isOpen, onClose, job }) => {
  if (!isOpen || !job) return null;

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/75 backdrop-blur-sm p-4 overflow-y-auto">
      <div className="relative w-full max-w-4xl bg-slate-900 border border-slate-700 rounded-2xl shadow-2xl overflow-hidden flex flex-col max-h-[90vh]">
        {/* Header */}
        <div className="flex items-center justify-between px-6 py-4 border-b border-slate-800 bg-slate-900/80 backdrop-blur">
          <div className="flex items-center gap-3">
            <div className="p-2 rounded-lg bg-blue-500/10 text-blue-400 font-bold text-sm">
              Serial #{job.priorityOrder}
            </div>
            <div>
              <h3 className="text-lg font-bold text-white flex items-center gap-2">
                {job.patientName}
                <span className="text-xs font-normal text-slate-400">({job.mobile})</span>
              </h3>
              <p className="text-xs text-slate-400">
                Scheduled Target: {job.targetTime} (Asia/Dhaka) • Date: {job.appointmentDate}
              </p>
            </div>
          </div>
          <button
            onClick={onClose}
            className="p-2 text-slate-400 hover:text-white rounded-lg hover:bg-slate-800 transition-colors"
          >
            <X className="w-5 h-5" />
          </button>
        </div>

        {/* Status Bar */}
        <div className="px-6 py-3 bg-slate-950/60 border-b border-slate-800 flex flex-wrap items-center justify-between gap-4 text-xs">
          <div className="flex items-center gap-2">
            <span className="text-slate-400">Execution Status:</span>
            <span
              className={`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full font-semibold ${
                job.status === "Success"
                  ? "bg-emerald-500/10 text-emerald-400 border border-emerald-500/20"
                  : job.status === "Manual Action Required"
                  ? "bg-amber-500/10 text-amber-400 border border-amber-500/20"
                  : job.status === "Running"
                  ? "bg-blue-500/10 text-blue-400 border border-blue-500/20 animate-pulse"
                  : "bg-rose-500/10 text-rose-400 border border-rose-500/20"
              }`}
            >
              {job.status === "Success" && <CheckCircle2 className="w-3.5 h-3.5" />}
              {job.status === "Manual Action Required" && <AlertTriangle className="w-3.5 h-3.5" />}
              {job.status === "Failed" && <XCircle className="w-3.5 h-3.5" />}
              {job.status === "Running" && <Clock className="w-3.5 h-3.5" />}
              {job.status}
            </span>
          </div>

          {job.resultSerial && (
            <div className="flex items-center gap-2">
              <span className="text-slate-400">Captured Serial:</span>
              <span className="font-bold text-emerald-400 bg-emerald-950/60 border border-emerald-800/50 px-2.5 py-0.5 rounded text-sm">
                {job.resultSerial}
              </span>
            </div>
          )}

          {job.confirmationNo && (
            <div className="flex items-center gap-2">
              <span className="text-slate-400">Confirmation Token:</span>
              <span className="font-mono text-cyan-400 bg-slate-800 px-2 py-0.5 rounded">
                {job.confirmationNo}
              </span>
            </div>
          )}
        </div>

        {/* Screenshot / Image Viewport */}
        <div className="flex-1 p-6 overflow-y-auto bg-slate-950 flex flex-col items-center justify-center min-h-[350px]">
          {job.screenshotPath ? (
            <div className="w-full max-w-3xl rounded-xl border border-slate-700 overflow-hidden shadow-2xl bg-slate-900">
              <img
                src={job.screenshotPath}
                alt={`Screenshot for ${job.patientName}`}
                className="w-full h-auto object-contain block"
              />
            </div>
          ) : (
            <div className="text-center py-12 text-slate-500">
              <Clock className="w-12 h-12 mx-auto mb-3 opacity-30" />
              <p className="font-medium text-slate-400">No screenshot captured yet</p>
              <p className="text-xs text-slate-600 mt-1">Screenshot will be saved upon browser worker execution</p>
            </div>
          )}

          {job.errorMessage && (
            <div className="w-full max-w-3xl mt-4 p-4 rounded-xl bg-rose-950/40 border border-rose-800/50 text-rose-300 text-sm">
              <div className="font-semibold flex items-center gap-2 mb-1 text-rose-400">
                <AlertTriangle className="w-4 h-4" /> Error Diagnostics
              </div>
              <p className="font-mono text-xs text-rose-200">{job.errorMessage}</p>
            </div>
          )}
        </div>

        {/* Footer */}
        <div className="px-6 py-4 border-t border-slate-800 bg-slate-900/80 flex items-center justify-between">
          <span className="text-xs text-slate-500 font-mono">Job ID #{job.id} • Audit Verification</span>
          <button
            onClick={onClose}
            className="px-4 py-2 bg-slate-800 hover:bg-slate-700 text-slate-200 text-sm font-medium rounded-lg transition-colors"
          >
            Close Inspector
          </button>
        </div>
      </div>
    </div>
  );
};
