"use client";

import { useState } from "react";
import { Card } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { ProgressCard } from "@/components/shared/ProgressCard";
import { ResultsTable, type ResultRow } from "@/components/shared/ResultsTable";
import { ExportButtons } from "@/components/shared/ExportButtons";
import { EmptyState } from "@/components/shared/EmptyState";
import { toast } from "sonner";
import { Search, ChevronRight } from "lucide-react";
import { useAppStore } from "@/lib/store";
import type { DecisionMakerCategory } from "@/lib/services/anymailfinder";
import type { ExportRow } from "@/lib/utils/csv";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Checkbox } from "@/components/ui/checkbox";

const RESULT_OPTIONS = [10, 25, 50, 100, 200];

const CATEGORY_OPTIONS: { value: DecisionMakerCategory; label: string }[] = [
  { value: "ceo", label: "CEO / Inhaber / Gründer" },
  { value: "operations", label: "COO / Geschäftsführung" },
  { value: "engineering", label: "CTO / Technik" },
  { value: "marketing", label: "CMO / Marketing" },
  { value: "finance", label: "CFO / Finanzen" },
  { value: "sales", label: "Vertriebsleiter" },
];

type Stage = "idle" | "running" | "done" | "failed";

export default function SerpPage() {
  const [query, setQuery] = useState("");
  const [numResults, setNumResults] = useState(50);
  const [country, setCountry] = useState("de");
  const [language, setLanguage] = useState("de");
  const [filterSocial, setFilterSocial] = useState(true);
  const [categories, setCategories] = useState<DecisionMakerCategory[]>(["ceo"]);
  const [stage, setStage] = useState<Stage>("idle");
  const [jobId, setJobId] = useState<string | null>(null);
  const [progress, setProgress] = useState({ current: 0, total: 0, phase: "" });
  const [results, setResults] = useState<ResultRow[]>([]);
  const { addJob, updateJob, removeJob } = useAppStore();

  // maxPages: since Google limits to 10 results/page, divide by 10
  const maxPages = Math.max(1, Math.ceil(numResults / 10));

  const startJob = async () => {
    if (!query.trim()) return toast.error("Suchbegriff eingeben");
    if (!categories.length) return toast.error("Mindestens eine Kategorie auswählen");

    setStage("running");
    setResults([]);
    setProgress({ current: 0, total: numResults, phase: "Google wird durchsucht..." });

    try {
      const res = await fetch("/api/jobs/serp-enrich", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          query,
          maxPages,
          countryCode: country,
          languageCode: language,
          filterSocial,
          categories,
        }),
      });
      const data = await res.json() as { jobId?: string; error?: string };
      if (!res.ok || !data.jobId) throw new Error(data.error || "Failed");

      setJobId(data.jobId);
      addJob({ id: data.jobId, type: "serp", status: "running", progress: 0, total: numResults });
      pollJob(data.jobId);
    } catch (err) {
      toast.error(err instanceof Error ? err.message : "Failed to start");
      setStage("failed");
    }
  };

  const pollJob = (id: string) => {
    let phase = "Google wird durchsucht...";
    const interval = setInterval(async () => {
      try {
        const res = await fetch(`/api/jobs/${id}/status`);
        const data = await res.json() as {
          status: string; totalLeads: number; emailsFound: number;
          results: ResultRow[];
        };

        // Infer phase from progress
        if (data.totalLeads > 0 && data.emailsFound === 0) phase = "Domains werden mit Anymailfinder angereichert...";
        if (data.emailsFound > 0) phase = `Bisher ${data.emailsFound} E-Mails gefunden...`;

        setProgress({ current: data.emailsFound, total: data.totalLeads || numResults, phase });
        if (data.results?.length) setResults(data.results);

        updateJob(id, { status: data.status, progress: data.emailsFound, total: data.totalLeads });

        if (data.status === "complete" || data.status === "failed") {
          clearInterval(interval);
          removeJob(id);
          setResults(data.results || []);
          if (data.status === "complete") {
            setStage("done");
            toast.success(`Fertig! ${data.emailsFound} E-Mails gefunden`);
          } else {
            setStage("failed");
            toast.error("Job fehlgeschlagen. Bitte API-Keys in den Einstellungen prüfen.");
          }
        }
      } catch {
        clearInterval(interval);
        setStage("failed");
      }
    }, 2000);
  };

  const exportRows: ExportRow[] = results.map(r => ({
    company_name: r.companyName,
    domain: r.domain,
    contact_name: r.contactName,
    contact_title: r.contactTitle,
    email: r.email,
    confidence_score: r.confidence !== undefined ? Math.round(r.confidence * 100) : undefined,
    source_tab: "serp",
    job_id: jobId || "",
    found_at: new Date().toISOString(),
  }));

  const emailsFound = results.filter(r => r.email).length;
  const hitRate = results.length > 0 ? Math.round((emailsFound / results.length) * 100) : 0;

  return (
    <div className="space-y-6 max-w-5xl">
      {/* Header */}
      <div className="relative rounded-2xl bg-gradient-to-r from-purple-600/10 to-blue-600/10 border border-[#1e1e2e] p-6 overflow-hidden">
        <div className="absolute inset-0 bg-gradient-to-br from-purple-500/5 to-transparent" />
        <div className="relative">
          <div className="flex items-center gap-2 text-sm text-purple-400 mb-2">
            <Search className="w-4 h-4" />
            <span>Tab 3</span>
            <ChevronRight className="w-3 h-3" />
            <span>Google SERP</span>
          </div>
          <h1 className="text-2xl font-bold text-white">SERP → E-Mail Anreicherung</h1>
          <p className="text-gray-400 mt-1 text-sm">
            Scrape Google-Suchergebnisse über Apify, extrahiere Domains und finde Entscheider-E-Mails.
          </p>
        </div>
      </div>

      {/* Step 1: Configure */}
      <Card className="bg-[#111118] border-[#1e1e2e] p-6 space-y-5">
        <h2 className="text-base font-semibold text-white flex items-center gap-2">
          <span className="w-6 h-6 rounded-full bg-purple-500/20 text-purple-400 text-xs flex items-center justify-center font-bold">1</span>
          Suchkonfiguration
        </h2>

        <div>
          <Label className="text-gray-300 text-sm mb-1.5 block">Suchbegriff</Label>
          <Input
            placeholder='e.g. "Solaranlage Installateur Deutschland"'
            value={query}
            onChange={e => setQuery(e.target.value)}
            className="bg-[#0d0d18] border-[#2e2e3e] text-white placeholder:text-gray-600 focus:border-blue-500"
          />
        </div>

        <div className="grid grid-cols-3 gap-4">
          <div>
            <Label className="text-gray-300 text-sm mb-1.5 block">Anzahl Ergebnisse</Label>
            <Select value={String(numResults)} onValueChange={v => setNumResults(Number(v))}>
              <SelectTrigger className="bg-[#0d0d18] border-[#2e2e3e] text-white">
                <SelectValue />
              </SelectTrigger>
              <SelectContent className="bg-[#111118] border-[#2e2e3e]">
                {RESULT_OPTIONS.map(n => (
                  <SelectItem key={n} value={String(n)} className="text-gray-300">{n} Ergebnisse</SelectItem>
                ))}
              </SelectContent>
            </Select>
            <p className="text-xs text-gray-500 mt-1">~{maxPages} Google-Seite{maxPages > 1 ? "n" : ""}</p>
          </div>
          <div>
            <Label className="text-gray-300 text-sm mb-1.5 block">Land</Label>
            <Select value={country} onValueChange={v => setCountry(v ?? "de")}>
              <SelectTrigger className="bg-[#0d0d18] border-[#2e2e3e] text-white">
                <SelectValue />
              </SelectTrigger>
              <SelectContent className="bg-[#111118] border-[#2e2e3e]">
                {[
                  { value: "de", label: "Deutschland (DE)" },
                  { value: "at", label: "Österreich (AT)" },
                  { value: "ch", label: "Schweiz (CH)" },
                  { value: "us", label: "USA (US)" },
                  { value: "gb", label: "Großbritannien (GB)" },
                ].map(c => (
                  <SelectItem key={c.value} value={c.value} className="text-gray-300">{c.label}</SelectItem>
                ))}
              </SelectContent>
            </Select>
          </div>
          <div>
            <Label className="text-gray-300 text-sm mb-1.5 block">Sprache</Label>
            <Select value={language} onValueChange={v => setLanguage(v ?? "de")}>
              <SelectTrigger className="bg-[#0d0d18] border-[#2e2e3e] text-white">
                <SelectValue />
              </SelectTrigger>
              <SelectContent className="bg-[#111118] border-[#2e2e3e]">
                {[
                  { value: "de", label: "Deutsch" },
                  { value: "en", label: "Englisch" },
                  { value: "fr", label: "Französisch" },
                ].map(l => (
                  <SelectItem key={l.value} value={l.value} className="text-gray-300">{l.label}</SelectItem>
                ))}
              </SelectContent>
            </Select>
          </div>
        </div>

        <div className="flex items-start gap-3">
          <Checkbox
            id="filterSocial"
            checked={filterSocial}
            onCheckedChange={v => setFilterSocial(!!v)}
            className="border-[#2e2e3e] mt-0.5"
          />
          <div>
            <label htmlFor="filterSocial" className="text-sm text-gray-300 cursor-pointer">
              Soziale Medien, Verzeichnisse & Aggregatoren ausschließen
            </label>
            <p className="text-xs text-gray-500 mt-0.5">
              Filtert: LinkedIn, Facebook, Instagram, Yelp, Wikipedia, Xing, Twitter/X, YouTube, Google Maps
            </p>
          </div>
        </div>
      </Card>

      {/* Step 2: Categories */}
      <Card className="bg-[#111118] border-[#1e1e2e] p-6 space-y-4">
        <h2 className="text-base font-semibold text-white flex items-center gap-2">
          <span className="w-6 h-6 rounded-full bg-purple-500/20 text-purple-400 text-xs flex items-center justify-center font-bold">2</span>
          Entscheider-Kategorien
        </h2>
        <div className="flex flex-wrap gap-2">
          {CATEGORY_OPTIONS.map(opt => (
            <button
              key={opt.value}
              onClick={() => setCategories(prev =>
                prev.includes(opt.value) ? prev.filter(c => c !== opt.value) : [...prev, opt.value]
              )}
              className={`px-3 py-1.5 rounded-lg text-sm font-medium border transition-all ${
                categories.includes(opt.value)
                  ? "bg-purple-500/20 text-purple-300 border-purple-500/40"
                  : "bg-[#0d0d18] text-gray-400 border-[#2e2e3e] hover:border-purple-500/30"
              }`}
            >
              {opt.label}
            </button>
          ))}
        </div>
      </Card>

      {/* Run */}
      <Card className="bg-[#111118] border-[#1e1e2e] p-6 space-y-4">
        <h2 className="text-base font-semibold text-white flex items-center gap-2">
          <span className="w-6 h-6 rounded-full bg-purple-500/20 text-purple-400 text-xs flex items-center justify-center font-bold">3</span>
          Pipeline starten
        </h2>

        {stage === "idle" || stage === "failed" ? (
          <Button
            onClick={startJob}
            disabled={!query.trim() || !categories.length}
            className="bg-gradient-to-r from-purple-500 to-blue-600 hover:from-purple-600 hover:to-blue-700 text-white font-medium px-8 shadow-lg hover:shadow-purple-500/25 transition-all"
          >
            SERP-Suche starten
          </Button>
        ) : stage === "running" ? (
          <ProgressCard
            title="SERP-Pipeline läuft..."
            current={progress.current}
            total={progress.total}
            subtitle={progress.phase}
            status="running"
          />
        ) : (
          <ProgressCard
            title="Pipeline abgeschlossen"
            current={emailsFound}
            total={results.length}
            subtitle={`Trefferquote: ${hitRate}% · ${results.length} Domains`}
            status="complete"
          />
        )}
      </Card>

      {/* Results */}
      {results.length > 0 && (
        <Card className="bg-[#111118] border-[#1e1e2e] p-6 space-y-4">
          <div className="flex items-center justify-between flex-wrap gap-3">
            <h2 className="text-base font-semibold text-white">Ergebnisse</h2>
            <ExportButtons
              rows={exportRows}
              filename={`serp-leads-${jobId?.slice(0, 8) || "export"}`}
              summary={`${emailsFound} E-Mails gefunden • ${hitRate}% Trefferquote`}
            />
          </div>
          <ResultsTable rows={results} loading={stage === "running" && results.length === 0} />
        </Card>
      )}

      {stage === "idle" && (
        <EmptyState
          icon={Search}
          title="SERP-Suche konfigurieren und starten"
          description="Suchbegriff eingeben, Zielkategorien auswählen und Starten drücken, um Leads aus Google-Suchergebnissen zu finden."
        />
      )}
    </div>
  );
}
