"use client";

import Link from "next/link";
import { useState } from "react";
import { useTableControls } from "@/components/ui/use-table-controls";
import { TableControls, SortableTh } from "@/components/ui/table-controls";
import { AddSiteButton, EditSiteButton } from "./site-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 { deleteSite, toggleSiteActive } from "./actions";

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

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

function SiteActionsMenu({ site }: { site: SiteRow }) {
  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)}>
          {site.isActive ? "Deactivate" : "Activate"}
        </RowActionsItem>
        <RowActionsSeparator />
        <RowActionsDestructiveItem onClick={() => setDeleteOpen(true)}>Delete</RowActionsDestructiveItem>
      </RowActionsMenu>

      <EditSiteButton
        site={{
          id: site.id,
          name: site.name,
          code: site.code,
          address: site.address,
          latitude: site.latitude,
          longitude: site.longitude,
          isActive: site.isActive,
        }}
        open={editOpen}
        onOpenChange={setEditOpen}
      />
      <DeleteConfirmDialog
        open={deleteOpen}
        onOpenChange={setDeleteOpen}
        label={site.name}
        onConfirm={() => deleteSite(site.id)}
      />
      <ConfirmDialog
        open={toggleOpen}
        onOpenChange={setToggleOpen}
        title={site.isActive ? `Deactivate ${site.name}?` : `Activate ${site.name}?`}
        description={site.isActive ? `${site.name} will be deactivated.` : `${site.name} will become active.`}
        confirmLabel={site.isActive ? "Deactivate" : "Activate"}
        onConfirm={() => toggleSiteActive(site.id)}
      />
    </>
  );
}

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">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 w-10"></th>}
            </tr>
          </thead>
          <tbody className="divide-y divide-neutral-100">
            {filtered.length === 0 && (
              <tr>
                <td colSpan={canEdit ? 7 : 6} 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.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">
                    <SiteActionsMenu site={site} />
                  </td>
                )}
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  );
}
