"use client";

import { useState } from "react";
import { useTableControls } from "@/components/ui/use-table-controls";
import { TableControls, SortableTh } from "@/components/ui/table-controls";
import { AddVesselButton, EditVesselButton } from "./vessel-form";
import { RowActionsMenu, RowActionsItem, RowActionsDestructiveItem, RowActionsSeparator } from "@/components/ui/row-actions-menu";
import { DeleteConfirmDialog } from "@/components/ui/delete-confirm-dialog";
import { ConfirmDialog } from "@/components/ui/confirm-dialog";
import { deleteVessel, toggleVesselActive } from "./actions";

export type VesselRow = {
  id: string;
  code: string;
  name: string;
  siteId: string | null;
  siteName: string | null;
  isActive: boolean;
};

type SiteOption = { id: string; name: string };

const CHIPS = ["Active", "Inactive"];

function VesselActionsMenu({ vessel, sites }: { vessel: VesselRow; sites: SiteOption[] }) {
  const [editOpen, setEditOpen] = useState(false);
  const [deleteOpen, setDeleteOpen] = useState(false);
  const [toggleOpen, setToggleOpen] = useState(false);

  return (
    <>
      <RowActionsMenu>
        <RowActionsItem onClick={() => setEditOpen(true)}>Edit</RowActionsItem>
        <RowActionsItem onClick={() => setToggleOpen(true)}>
          {vessel.isActive ? "Deactivate" : "Activate"}
        </RowActionsItem>
        <RowActionsSeparator />
        <RowActionsDestructiveItem onClick={() => setDeleteOpen(true)}>Delete</RowActionsDestructiveItem>
      </RowActionsMenu>

      <EditVesselButton
        vessel={{ id: vessel.id, name: vessel.name, code: vessel.code, siteId: vessel.siteId, isActive: vessel.isActive }}
        sites={sites}
        open={editOpen}
        onOpenChange={setEditOpen}
      />
      <DeleteConfirmDialog
        open={deleteOpen}
        onOpenChange={setDeleteOpen}
        label={vessel.name}
        onConfirm={() => deleteVessel(vessel.id)}
      />
      <ConfirmDialog
        open={toggleOpen}
        onOpenChange={setToggleOpen}
        title={vessel.isActive ? `Deactivate ${vessel.name}?` : `Activate ${vessel.name}?`}
        description={vessel.isActive ? `${vessel.name} will be hidden from new purchase orders.` : `${vessel.name} will become available for new purchase orders.`}
        confirmLabel={vessel.isActive ? "Deactivate" : "Activate"}
        onConfirm={() => toggleVesselActive(vessel.id)}
      />
    </>
  );
}

export function VesselsTable({ vessels, sites }: { vessels: VesselRow[]; sites: SiteOption[] }) {
  const { search, setSearch, sortKey, sortDir, toggleSort, activeFilters, toggleFilter, filtered } =
    useTableControls<VesselRow>({
      rows: vessels,
      defaultSortKey: "code",
      searchText: (v) =>
        [v.code, v.name, v.siteName ?? "", v.isActive ? "active" : "inactive"].join(" "),
      chipMatch: (v, chip) => {
        if (chip.toLowerCase() === "active") return v.isActive;
        if (chip.toLowerCase() === "inactive") return !v.isActive;
        return false;
      },
      sortValue: (v, key) => {
        if (key === "isActive") return v.isActive ? "Active" : "Inactive";
        if (key === "siteName") return v.siteName ?? "";
        const val = v[key as keyof VesselRow];
        if (val === null || val === undefined) return "";
        return typeof val === "string" || typeof val === "number" || typeof val === "boolean" ? val : String(val);
      },
    });

  return (
    <div>
      <div className="mb-6 flex items-center justify-between">
        <h1 className="text-2xl font-semibold text-neutral-900">Vessel Management</h1>
        <AddVesselButton sites={sites} />
      </div>

      <TableControls
        search={search}
        onSearch={setSearch}
        searchPlaceholder="Search vessels…"
        chips={CHIPS}
        activeFilters={activeFilters}
        onToggleFilter={toggleFilter}
      />

      <div className="rounded-lg border border-neutral-200 bg-white overflow-hidden">
        <table className="w-full text-sm">
          <thead className="bg-neutral-50 border-b border-neutral-200">
            <tr>
              <SortableTh sortKey="code" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof VesselRow)}>Code</SortableTh>
              <SortableTh sortKey="name" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof VesselRow)}>Name</SortableTh>
              <SortableTh sortKey="siteName" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof VesselRow)}>Site</SortableTh>
              <SortableTh sortKey="isActive" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof VesselRow)}>Status</SortableTh>
              <th className="px-4 py-3 w-10"></th>
            </tr>
          </thead>
          <tbody className="divide-y divide-neutral-100">
            {filtered.length === 0 && (
              <tr>
                <td colSpan={5} className="px-4 py-8 text-center text-neutral-400">
                  No vessels match your search.
                </td>
              </tr>
            )}
            {filtered.map((vessel) => (
              <tr key={vessel.id} className="hover:bg-neutral-50">
                <td className="px-4 py-3 font-mono text-xs text-neutral-600">{vessel.code}</td>
                <td className="px-4 py-3 font-medium text-neutral-900">{vessel.name}</td>
                <td className="px-4 py-3 text-neutral-500">
                  {vessel.siteName ?? <span className="italic text-neutral-400">—</span>}
                </td>
                <td className="px-4 py-3">
                  <span className={`rounded-full px-2.5 py-0.5 text-xs font-medium ${
                    vessel.isActive ? "bg-success-100 text-success-700" : "bg-neutral-100 text-neutral-500"
                  }`}>
                    {vessel.isActive ? "Active" : "Inactive"}
                  </span>
                </td>
                <td className="px-4 py-3">
                  <VesselActionsMenu vessel={vessel} sites={sites} />
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  );
}
