"use client";

import Link from "next/link";
import { useTableControls } from "@/components/ui/use-table-controls";
import { TableControls, SortableTh } from "@/components/ui/table-controls";
import { AddSiteButton, EditSiteButton } from "./site-form";
import { ConfirmDeleteButton } from "@/components/ui/confirm-delete-button";
import { deleteSite } from "./actions";

export type SiteRow = {
  id: string;
  code: string;
  name: string;
  address: string | null;
  latitude: number | null;
  longitude: number | null;
  isActive: boolean;
  vesselCount: number;
  inventoryCount: number;
};

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

export function SitesTable({
  sites,
  canEdit,
}: {
  sites: SiteRow[];
  canEdit: boolean;
}) {
  const { search, setSearch, sortKey, sortDir, toggleSort, activeFilters, toggleFilter, filtered } =
    useTableControls<SiteRow>({
      rows: sites,
      defaultSortKey: "name",
      searchText: (s) =>
        [s.code, s.name, s.address ?? "", s.isActive ? "active" : "inactive"].join(" "),
      chipMatch: (s, chip) => {
        if (chip.toLowerCase() === "active") return s.isActive;
        if (chip.toLowerCase() === "inactive") return !s.isActive;
        return false;
      },
      sortValue: (s, key) => {
        if (key === "isActive") return s.isActive ? "Active" : "Inactive";
        const val = s[key as keyof SiteRow];
        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">
        <div>
          <h1 className="text-2xl font-semibold text-neutral-900">Sites</h1>
          <p className="text-sm text-neutral-500 mt-0.5">Ports, depots and offices with inventory</p>
        </div>
        {canEdit && <AddSiteButton />}
      </div>

      <TableControls
        search={search}
        onSearch={setSearch}
        searchPlaceholder="Search sites…"
        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="name" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof SiteRow)}>Name</SortableTh>
              <SortableTh sortKey="code" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof SiteRow)}>Code</SortableTh>
              <SortableTh sortKey="address" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof SiteRow)}>Address</SortableTh>
              <th className="px-4 py-3 text-right font-medium text-neutral-600">Cost Centres</th>
              <th className="px-4 py-3 text-right font-medium text-neutral-600">Items tracked</th>
              <th className="px-4 py-3 text-left font-medium text-neutral-600">Location</th>
              <SortableTh sortKey="isActive" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof SiteRow)}>Status</SortableTh>
              {canEdit && <th className="px-4 py-3"></th>}
            </tr>
          </thead>
          <tbody className="divide-y divide-neutral-100">
            {filtered.length === 0 && (
              <tr>
                <td colSpan={canEdit ? 8 : 7} className="px-4 py-8 text-center text-neutral-400">
                  No sites match your search.
                </td>
              </tr>
            )}
            {filtered.map((site) => (
              <tr key={site.id} className="hover:bg-neutral-50">
                <td className="px-4 py-3">
                  <Link href={`/admin/sites/${site.id}`} className="font-medium text-primary-600 hover:underline">
                    {site.name}
                  </Link>
                </td>
                <td className="px-4 py-3 font-mono text-xs text-neutral-500">{site.code}</td>
                <td className="px-4 py-3 text-neutral-500 max-w-xs truncate">
                  {site.address ?? <span className="italic text-neutral-400">—</span>}
                </td>
                <td className="px-4 py-3 text-right text-neutral-600">
                  {site.vesselCount || <span className="text-neutral-400">—</span>}
                </td>
                <td className="px-4 py-3 text-right text-neutral-600">
                  {site.inventoryCount || <span className="text-neutral-400">—</span>}
                </td>
                <td className="px-4 py-3 text-xs text-neutral-500">
                  {site.latitude && site.longitude
                    ? `${site.latitude.toFixed(4)}, ${site.longitude.toFixed(4)}`
                    : <span className="italic text-neutral-400">Not set</span>}
                </td>
                <td className="px-4 py-3">
                  <span className={`rounded-full px-2.5 py-0.5 text-xs font-medium ${
                    site.isActive ? "bg-success-100 text-success-700" : "bg-neutral-100 text-neutral-500"
                  }`}>
                    {site.isActive ? "Active" : "Inactive"}
                  </span>
                </td>
                {canEdit && (
                  <td className="px-4 py-3">
                    <span className="flex items-center gap-3">
                      <EditSiteButton site={{
                        id: site.id,
                        name: site.name,
                        code: site.code,
                        address: site.address,
                        latitude: site.latitude,
                        longitude: site.longitude,
                        isActive: site.isActive,
                      }} />
                      <ConfirmDeleteButton onDelete={deleteSite.bind(null, site.id)} label={site.name} />
                    </span>
                  </td>
                )}
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  );
}
