"use client";
import { useState } from "react";
import {
  PRIORITEITEN,
  STATUSSEN,
  categorieLabels,
  prioriteitLabels,
  statusLabels,
  PRIORITEIT_STIJL,
  STATUS_STIJL,
  CATEGORIE_STIJL,
  type Verbetervoorstel,
  type VoorstelPrioriteit,
  type VoorstelStatus,
} from "@/data/verbetervoorstellen";

export function VoorstelKaart({
  voorstel,
  onBewerk,
  onSla,
  onVerwijder,
}: {
  voorstel: Verbetervoorstel;
  onBewerk: () => void;
  onSla: (v: Verbetervoorstel) => Promise<void>;
  onVerwijder: (id: string) => Promise<void>;
}) {
  const [open, setOpen] = useState(false);
  const [bezig, setBezig] = useState(false);

  const heeftDetails = !!(voorstel.technischeDetails || voorstel.acceptatiecriteria);

  async function wijzigPrioriteit(p: VoorstelPrioriteit) {
    setBezig(true);
    await onSla({ ...voorstel, prioriteit: p, aangepast: new Date().toISOString().slice(0, 10) });
    setBezig(false);
  }

  async function wijzigStatus(s: VoorstelStatus) {
    setBezig(true);
    await onSla({ ...voorstel, status: s, aangepast: new Date().toISOString().slice(0, 10) });
    setBezig(false);
  }

  const isAfgesloten = voorstel.status === "gedaan" || voorstel.status === "afgewezen";

  return (
    <div className={`bg-white border rounded-xl shadow-sm overflow-hidden transition-opacity ${isAfgesloten ? "opacity-60" : ""} border-slate-200`}>
      {/* Header */}
      <div className="px-5 py-3 flex items-start gap-3">
        <div className="flex flex-col gap-1.5 shrink-0 pt-0.5">
          <span className={`text-xs px-2 py-0.5 rounded border font-medium ${PRIORITEIT_STIJL[voorstel.prioriteit]}`}>
            {prioriteitLabels[voorstel.prioriteit]}
          </span>
          <span className={`text-xs px-2 py-0.5 rounded border font-medium ${STATUS_STIJL[voorstel.status]}`}>
            {statusLabels[voorstel.status]}
          </span>
        </div>

        <div className="flex-1 min-w-0">
          <div className="flex items-center gap-2 flex-wrap">
            <p className="text-sm font-semibold text-slate-800">{voorstel.titel}</p>
            <span className={`text-xs px-1.5 py-0.5 rounded font-medium ${CATEGORIE_STIJL[voorstel.categorie]}`}>
              {categorieLabels[voorstel.categorie]}
            </span>
          </div>
          <p className="text-sm text-slate-600 mt-0.5 line-clamp-2">{voorstel.omschrijving}</p>
          <p className="text-xs text-slate-400 mt-1">{voorstel.datum}</p>
        </div>

        {heeftDetails && (
          <button
            onClick={() => setOpen((o) => !o)}
            className="text-slate-400 text-sm shrink-0 hover:text-slate-600"
            title="Toon technische details"
          >
            {open ? "▴" : "▾"}
          </button>
        )}
      </div>

      {/* Technische details */}
      {open && heeftDetails && (
        <div className="border-t border-slate-100 px-5 py-3 space-y-2 bg-slate-50">
          {voorstel.technischeDetails && (
            <div>
              <p className="text-xs font-semibold text-slate-500 uppercase tracking-wide mb-0.5">Technische details</p>
              <p className="text-sm text-slate-700 whitespace-pre-wrap">{voorstel.technischeDetails}</p>
            </div>
          )}
          {voorstel.acceptatiecriteria && (
            <div>
              <p className="text-xs font-semibold text-slate-500 uppercase tracking-wide mb-0.5">Klaar als</p>
              <p className="text-sm text-slate-700 whitespace-pre-wrap">{voorstel.acceptatiecriteria}</p>
            </div>
          )}
        </div>
      )}

      {/* Acties */}
      <div className="border-t border-slate-100 px-5 py-2.5 flex items-center gap-2 flex-wrap">
        {/* Prioriteit */}
        <div className="flex items-center gap-1">
          <span className="text-xs text-slate-400 mr-0.5">Prioriteit:</span>
          {PRIORITEITEN.map((p) => (
            <button
              key={p}
              onClick={() => wijzigPrioriteit(p)}
              disabled={bezig || voorstel.prioriteit === p}
              className={`text-xs px-2 py-0.5 rounded border transition-colors ${
                voorstel.prioriteit === p
                  ? PRIORITEIT_STIJL[p] + " cursor-default"
                  : "border-slate-200 text-slate-500 hover:border-slate-300 hover:bg-slate-50"
              }`}
            >
              {prioriteitLabels[p]}
            </button>
          ))}
        </div>

        <div className="h-4 w-px bg-slate-200 mx-1" />

        {/* Status */}
        <div className="flex items-center gap-1">
          <span className="text-xs text-slate-400 mr-0.5">Status:</span>
          {STATUSSEN.map((s) => (
            <button
              key={s}
              onClick={() => wijzigStatus(s)}
              disabled={bezig || voorstel.status === s}
              className={`text-xs px-2 py-0.5 rounded border transition-colors ${
                voorstel.status === s
                  ? STATUS_STIJL[s] + " cursor-default"
                  : "border-slate-200 text-slate-500 hover:border-slate-300 hover:bg-slate-50"
              }`}
            >
              {statusLabels[s]}
            </button>
          ))}
        </div>

        <div className="ml-auto flex gap-1.5">
          <button
            onClick={onBewerk}
            className="text-xs px-3 py-1.5 rounded-lg border border-slate-200 text-slate-600 hover:bg-slate-50"
          >
            ✏️ Bewerk
          </button>
          <button
            onClick={() => onVerwijder(voorstel.id)}
            disabled={bezig}
            className="text-xs px-3 py-1.5 rounded-lg border border-red-200 text-red-500 hover:bg-red-50 disabled:opacity-50"
          >
            ✕
          </button>
        </div>
      </div>
    </div>
  );
}
