"use client";

import { GripVertical } from "lucide-react";
import { cn } from "@/lib/utils";
import type { ColumnConfig } from "@/hooks/useTransactionSettings";
import { Switch } from "@/components/ui/switch";

export interface TransactionColumnListProps {
  title: string;
  columns: ColumnConfig[];
  dragColId: string | null;
  onDragStart: (id: string) => void;
  onDrop: (targetId: string) => void;
  onToggleVisibility: (id: string) => void;
}

export function TransactionColumnList({
  title,
  columns,
  dragColId,
  onDragStart,
  onDrop,
  onToggleVisibility,
}: TransactionColumnListProps) {
  return (
    <div className="animate-fadeIn rounded-lg border border-border bg-card p-5 shadow-lg">
      <h2 className="mb-3 text-xl font-semibold text-foreground">{title}</h2>
      <p className="mb-4 text-xs text-muted-foreground">
        Drag to reorder • Use checkbox to show/hide a column
      </p>

      {columns.length === 0 ? (
        <p className="text-sm text-muted-foreground">No columns to configure.</p>
      ) : (
        <ul className="space-y-2 text-sm" role="list">
          {columns.map((col) => (
            <li
              key={col.id}
              draggable
              onDragStart={() => onDragStart(col.id)}
              onDragOver={(e) => e.preventDefault()}
              onDrop={() => onDrop(col.id)}
              className={cn(
                "flex cursor-move items-center justify-between gap-3 rounded-lg border border-border bg-muted px-3 py-2",
                dragColId === col.id && "ring-2 ring-indigo-400"
              )}
            >
              <div className="flex items-center gap-3">
                <GripVertical className="h-4 w-4 text-muted-foreground" />
                <span className="font-medium text-foreground">{col.label}</span>
              </div>
              <div className="flex items-center gap-2">
                {/* A visible/hidden pair is a toggle, not a tick box — and a
                    native checkbox draws its own OS checkmark, so it could
                    never take the brand colour. */}
                <span className="w-12 text-right text-xs text-muted-foreground">
                  {col.visible ? "Visible" : "Hidden"}
                </span>
                <Switch
                  checked={col.visible}
                  onCheckedChange={() => onToggleVisibility(col.id)}
                  aria-label={`Toggle ${col.label} column visibility`}
                />
              </div>
            </li>
          ))}
        </ul>
      )}
    </div>
  );
}
