'use client';

import { useState } from 'react';
import { ChevronDown, Trash2 } from 'lucide-react';

interface Chart {
  id: number;
  name: string;
  created_at: number;
}

interface ChartSelectorProps {
  charts: Chart[];
  activeChartId: number | null;
  onSelectChart: (chartId: number) => void;
  onDeleteChart: (chartId: number) => void;
}

export default function ChartSelector({
  charts,
  activeChartId,
  onSelectChart,
  onDeleteChart,
}: ChartSelectorProps) {
  const [isOpen, setIsOpen] = useState(false);
  const [confirmDeleteId, setConfirmDeleteId] = useState<number | null>(null);

  const activeChart = charts.find((c) => c.id === activeChartId);

  if (charts.length === 0) {
    return (
      <p className="text-[10px] text-muted-foreground italic">
        No charts — upload a CSV to get started
      </p>
    );
  }

  return (
    <div className="relative">
      <button
        onClick={() => setIsOpen(!isOpen)}
        className="w-full flex items-center justify-between px-2 py-1.5 text-xs rounded bg-secondary/50 hover:bg-secondary text-foreground transition-colors"
      >
        <span className="font-mono font-medium truncate">{activeChart?.name || 'Select chart'}</span>
        <ChevronDown className={`w-3 h-3 transition-transform ${isOpen ? 'rotate-180' : ''}`} />
      </button>

      {isOpen && (
        <div className="absolute z-50 mt-1 w-full rounded-md border border-border bg-popover shadow-md max-h-48 overflow-y-auto">
          {charts.map((chart) => (
            <div
              key={chart.id}
              className={`flex items-center justify-between px-2 py-1.5 text-xs hover:bg-accent cursor-pointer ${
                chart.id === activeChartId ? 'bg-accent/50' : ''
              }`}
            >
              <div
                className="flex-1 min-w-0"
                onClick={() => {
                  onSelectChart(chart.id);
                  setIsOpen(false);
                }}
              >
                <div className="truncate font-mono text-foreground">{chart.name}</div>
              </div>
              <button
                onClick={(e) => {
                  e.stopPropagation();
                  setConfirmDeleteId(chart.id);
                }}
                className="ml-2 p-0.5 text-muted-foreground hover:text-destructive flex-shrink-0"
                title="Delete chart"
              >
                <Trash2 className="h-3 w-3" />
              </button>
            </div>
          ))}
        </div>
      )}

      {confirmDeleteId !== null && (
        <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
          <div className="bg-popover border border-border rounded-lg p-4 max-w-sm mx-4 shadow-lg">
            <p className="text-xs text-foreground">
              Delete &ldquo;{charts.find((c) => c.id === confirmDeleteId)?.name}&rdquo; and all its data? This cannot be undone.
            </p>
            <div className="flex justify-end gap-2 mt-3">
              <button
                onClick={() => setConfirmDeleteId(null)}
                className="px-3 py-1 text-xs rounded border border-border hover:bg-accent text-foreground"
              >
                Cancel
              </button>
              <button
                onClick={() => {
                  onDeleteChart(confirmDeleteId);
                  setConfirmDeleteId(null);
                  setIsOpen(false);
                }}
                className="px-3 py-1 text-xs rounded bg-destructive text-destructive-foreground hover:bg-destructive/90"
              >
                Delete
              </button>
            </div>
          </div>
        </div>
      )}
    </div>
  );
}
