import { createFileRoute, useNavigate } from "@tanstack/react-router";
import { useCallback, useRef, useState } from "react";
import {
  Upload as UploadIcon,
  FileSpreadsheet,
  Loader2,
  CheckCircle2,
  XCircle,
  SlidersHorizontal,
  ArrowRight,
} from "lucide-react";
import {
  analyze,
  parseFile,
  detectMapping,
  DEFAULT_THRESHOLDS,
  type DetectionThresholds,
  type FieldMapping,
} from "@/lib/electrical";
import { saveResult } from "@/lib/history";
import { cn } from "@/lib/utils";

export const Route = createFileRoute("/upload")({
  component: UploadPage,
});

type Status = "idle" | "parsing" | "ready" | "error";

const MAPPING_ROWS: { key: keyof FieldMapping; label: string }[] = [
  { key: "timestamp", label: "Timestamp" },
  { key: "currentR", label: "Current R" },
  { key: "currentS", label: "Current S" },
  { key: "currentT", label: "Current T" },
  { key: "voltageR", label: "Voltage R" },
  { key: "voltageS", label: "Voltage S" },
  { key: "voltageT", label: "Voltage T" },
];

function UploadPage() {
  const navigate = useNavigate();
  const inputRef = useRef<HTMLInputElement>(null);
  const [status, setStatus] = useState<Status>("idle");
  const [dragOver, setDragOver] = useState(false);
  const [file, setFile] = useState<File | null>(null);
  const [error, setError] = useState<string | null>(null);
  const [parsed, setParsed] = useState<Awaited<ReturnType<typeof parseFile>> | null>(null);
  const [mapping, setMapping] = useState<FieldMapping | null>(null);
  const [thresholds, setThresholds] = useState<DetectionThresholds>(DEFAULT_THRESHOLDS);
  const [analyzing, setAnalyzing] = useState(false);

  const handleFile = useCallback(async (f: File) => {
    setFile(f);
    setError(null);
    setStatus("parsing");
    try {
      const p = await parseFile(f);
      if (!p.records.length) throw new Error("No rows found in this file.");
      const m = detectMapping(p.headers);
      setParsed(p);
      setMapping(m);
      setStatus("ready");
    } catch (e) {
      setError(e instanceof Error ? e.message : "Could not read this file.");
      setStatus("error");
    }
  }, []);

  const onDrop = useCallback(
    (e: React.DragEvent) => {
      e.preventDefault();
      setDragOver(false);
      const f = e.dataTransfer.files?.[0];
      if (f) handleFile(f);
    },
    [handleFile],
  );

  const runAnalysis = useCallback(async () => {
    if (!parsed || !file || !mapping) return;
    setAnalyzing(true);
    // Yield so the spinner paints before heavy work.
    await new Promise((r) => setTimeout(r, 30));
    try {
      const result = analyze(parsed, file.name, thresholds, mapping);
      saveResult(result);
      navigate({ to: "/analysis/$id", params: { id: result.id } });
    } catch (e) {
      setError(e instanceof Error ? e.message : "Analysis failed.");
      setStatus("error");
      setAnalyzing(false);
    }
  }, [parsed, file, mapping, thresholds, navigate]);

  const mappedCount = mapping
    ? MAPPING_ROWS.filter((r) => mapping[r.key]).length
    : 0;
  const hasElectrical = mapping
    ? Boolean(mapping.currentR || mapping.voltageR || mapping.currentS || mapping.voltageS)
    : false;

  return (
    <div className="space-y-6">
      <div>
        <h1 className="font-display text-2xl font-bold text-foreground sm:text-3xl">Upload data</h1>
        <p className="mt-1 text-sm text-muted-foreground">
          Drop a CSV or Excel meter export. We detect the columns and run the anomaly engine for you.
        </p>
      </div>

      {/* Dropzone */}
      <div
        role="button"
        tabIndex={0}
        onClick={() => inputRef.current?.click()}
        onKeyDown={(e) => (e.key === "Enter" || e.key === " ") && inputRef.current?.click()}
        onDragOver={(e) => {
          e.preventDefault();
          setDragOver(true);
        }}
        onDragLeave={() => setDragOver(false)}
        onDrop={onDrop}
        className={cn(
          "flex cursor-pointer flex-col items-center justify-center gap-3 rounded-2xl border-2 border-dashed p-8 text-center transition-colors sm:p-12",
          dragOver
            ? "border-primary bg-primary/5"
            : "border-border bg-card hover:border-primary/50 hover:bg-secondary/40",
        )}
      >
        <input
          ref={inputRef}
          type="file"
          accept=".csv,.xlsx,.xls,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          className="hidden"
          onChange={(e) => e.target.files?.[0] && handleFile(e.target.files[0])}
        />
        <span className="flex h-14 w-14 items-center justify-center rounded-full bg-primary/10 text-primary">
          <UploadIcon className="h-7 w-7" />
        </span>
        <div>
          <p className="font-display text-lg font-semibold text-foreground">
            Tap to choose a file
          </p>
          <p className="text-sm text-muted-foreground">or drag &amp; drop · .csv, .xlsx, .xls</p>
        </div>
      </div>

      {/* File state */}
      {file && (
        <div className="flex items-center gap-3 rounded-xl border border-border bg-card p-4 shadow-[var(--shadow-panel)]">
          <span className="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-secondary text-secondary-foreground">
            <FileSpreadsheet className="h-5 w-5" />
          </span>
          <div className="min-w-0 flex-1">
            <p className="truncate font-medium text-foreground">{file.name}</p>
            <p className="text-xs text-muted-foreground">
              {(file.size / 1024).toFixed(0)} KB
              {parsed ? ` · ${parsed.records.length.toLocaleString()} rows` : ""}
            </p>
          </div>
          {status === "parsing" && <Loader2 className="h-5 w-5 animate-spin text-primary" />}
          {status === "ready" && <CheckCircle2 className="h-5 w-5 text-success" />}
          {status === "error" && <XCircle className="h-5 w-5 text-destructive" />}
        </div>
      )}

      {error && (
        <div className="flex items-start gap-2 rounded-xl border border-destructive/30 bg-destructive/5 p-4 text-sm text-destructive">
          <XCircle className="mt-0.5 h-4 w-4 shrink-0" />
          {error}
        </div>
      )}

      {/* Detected mapping */}
      {status === "ready" && mapping && (
        <div className="rounded-xl border border-border bg-card p-5 shadow-[var(--shadow-panel)]">
          <div className="mb-3 flex items-center justify-between">
            <h2 className="font-display text-lg font-semibold text-foreground">Detected columns</h2>
            <span className="text-xs font-medium text-muted-foreground">
              {mappedCount}/{MAPPING_ROWS.length} matched
            </span>
          </div>
          <div className="grid gap-2 sm:grid-cols-2">
            {MAPPING_ROWS.map((r) => {
              const val = mapping[r.key];
              return (
                <div
                  key={r.key}
                  className="flex items-center justify-between gap-2 rounded-lg border border-border bg-background px-3 py-2"
                >
                  <span className="text-sm font-medium text-foreground">{r.label}</span>
                  {val ? (
                    <span className="flex items-center gap-1.5 truncate font-mono text-xs text-muted-foreground">
                      <CheckCircle2 className="h-3.5 w-3.5 shrink-0 text-success" />
                      <span className="truncate">{val}</span>
                    </span>
                  ) : (
                    <span className="text-xs italic text-muted-foreground">not found</span>
                  )}
                </div>
              );
            })}
          </div>
          {!hasElectrical && (
            <p className="mt-3 rounded-lg bg-warning/10 px-3 py-2 text-xs text-warning-foreground">
              No current or voltage columns were detected. Analysis may be empty — check that the
              file has phase current/voltage columns.
            </p>
          )}
        </div>
      )}

      {/* Thresholds */}
      {status === "ready" && (
        <div className="rounded-xl border border-border bg-card p-5 shadow-[var(--shadow-panel)]">
          <div className="mb-4 flex items-center gap-2">
            <SlidersHorizontal className="h-4 w-4 text-primary" />
            <h2 className="font-display text-lg font-semibold text-foreground">Detection thresholds</h2>
          </div>
          <div className="grid gap-4 sm:grid-cols-2">
            <ThresholdField
              label="Unbalance warning (%)"
              value={thresholds.unbalanceWarn}
              onChange={(v) => setThresholds((t) => ({ ...t, unbalanceWarn: v }))}
            />
            <ThresholdField
              label="Unbalance critical (%)"
              value={thresholds.unbalanceCritical}
              onChange={(v) => setThresholds((t) => ({ ...t, unbalanceCritical: v }))}
            />
            <ThresholdField
              label="Under-voltage warning (% below ref)"
              value={thresholds.underVoltageWarnPct}
              onChange={(v) => setThresholds((t) => ({ ...t, underVoltageWarnPct: v }))}
            />
            <ThresholdField
              label="Under-voltage critical (% below ref)"
              value={thresholds.underVoltageCriticalPct}
              onChange={(v) => setThresholds((t) => ({ ...t, underVoltageCriticalPct: v }))}
            />
          </div>
        </div>
      )}

      {status === "ready" && (
        <button
          onClick={runAnalysis}
          disabled={analyzing}
          className="inline-flex w-full items-center justify-center gap-2 rounded-xl bg-primary px-6 py-4 text-base font-semibold text-primary-foreground shadow-[var(--shadow-glow)] transition-transform hover:scale-[1.01] disabled:opacity-70 sm:w-auto"
        >
          {analyzing ? (
            <>
              <Loader2 className="h-5 w-5 animate-spin" />
              Analyzing {parsed?.records.length.toLocaleString()} rows…
            </>
          ) : (
            <>
              Run anomaly detection
              <ArrowRight className="h-5 w-5" />
            </>
          )}
        </button>
      )}
    </div>
  );
}

function ThresholdField({
  label,
  value,
  onChange,
}: {
  label: string;
  value: number;
  onChange: (v: number) => void;
}) {
  return (
    <label className="block">
      <span className="mb-1.5 block text-sm font-medium text-foreground">{label}</span>
      <input
        type="number"
        min={0}
        step={0.5}
        value={value}
        onChange={(e) => onChange(Number(e.target.value))}
        className="w-full rounded-lg border border-input bg-background px-3 py-2.5 text-sm text-foreground outline-none focus:border-primary focus:ring-2 focus:ring-ring/30"
      />
    </label>
  );
}
