"use client";

import { useState } from "react";
import { useTableControls } from "@/components/ui/use-table-controls";
import { TableControls, SortableTh } from "@/components/ui/table-controls";
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 {
  AddDeliveryLocationButton,
  EditDeliveryLocationButton,
  type CompanyOption,
  type DeliveryLocationRow,
} from "./delivery-location-form";
import { deleteDeliveryLocation, toggleDeliveryLocationActive } from "./actions";

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

function LocationActionsMenu({ companies, location }: { companies: CompanyOption[]; location: DeliveryLocationRow }) {
  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)}>
          {location.isActive ? "Deactivate" : "Activate"}
        </RowActionsItem>
        <RowActionsSeparator />
        <RowActionsDestructiveItem onClick={() => setDeleteOpen(true)}>Delete</RowActionsDestructiveItem>
      </RowActionsMenu>

      <EditDeliveryLocationButton companies={companies} location={location} open={editOpen} onOpenChange={setEditOpen} />
      <DeleteConfirmDialog
        open={deleteOpen}
        onOpenChange={setDeleteOpen}
        label={`${location.companyName} — ${location.address}`}
        onConfirm={() => deleteDeliveryLocation(location.id)}
      />
      <ConfirmDialog
        open={toggleOpen}
        onOpenChange={setToggleOpen}
        title={location.isActive ? "Deactivate location?" : "Activate location?"}
        description={
          location.isActive
            ? "It will no longer appear in the Place of Delivery dropdown."
            : "It will appear in the Place of Delivery dropdown again."
        }
        confirmLabel={location.isActive ? "Deactivate" : "Activate"}
        onConfirm={() => toggleDeliveryLocationActive(location.id)}
      />
    </>
  );
}

export function DeliveryLocationsTable({
  locations,
  companies,
}: {
  locations: DeliveryLocationRow[];
  companies: CompanyOption[];
}) {
  const { search, setSearch, sortKey, sortDir, toggleSort, activeFilters, toggleFilter, filtered } =
    useTableControls<DeliveryLocationRow>({
      rows: locations,
      defaultSortKey: "companyName",
      searchText: (l) => [l.companyName, l.address, l.isActive ? "active" : "inactive"].join(" "),
      chipMatch: (l, chip) => {
        if (chip.toLowerCase() === "active") return l.isActive;
        if (chip.toLowerCase() === "inactive") return !l.isActive;
        return false;
      },
      sortValue: (l, key) => {
        if (key === "isActive") return l.isActive ? "Active" : "Inactive";
        const val = l[key as keyof DeliveryLocationRow];
        return typeof val === "string" || 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">Delivery Locations</h1>
          <p className="text-sm text-neutral-500 mt-0.5">Destinations that populate the PO &ldquo;Place of Delivery&rdquo; dropdown</p>
        </div>
        <AddDeliveryLocationButton companies={companies} />
      </div>

      <TableControls
        search={search}
        onSearch={setSearch}
        searchPlaceholder="Search company or address…"
        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="companyName" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof DeliveryLocationRow)}>Company</SortableTh>
              <SortableTh sortKey="address" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof DeliveryLocationRow)}>Address</SortableTh>
              <SortableTh sortKey="isActive" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof DeliveryLocationRow)}>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={4} className="px-4 py-8 text-center text-neutral-400">
                  No delivery locations yet. Add one to populate the Place of Delivery dropdown.
                </td>
              </tr>
            )}
            {filtered.map((location) => (
              <tr key={location.id} className="hover:bg-neutral-50">
                <td className="px-4 py-3 font-medium text-neutral-900">{location.companyName}</td>
                <td className="px-4 py-3 text-neutral-600 max-w-md whitespace-pre-wrap">{location.address}</td>
                <td className="px-4 py-3">
                  <span className={`rounded-full px-2.5 py-0.5 text-xs font-medium ${
                    location.isActive ? "bg-success-100 text-success-700" : "bg-neutral-100 text-neutral-500"
                  }`}>
                    {location.isActive ? "Active" : "Inactive"}
                  </span>
                </td>
                <td className="px-4 py-3">
                  <LocationActionsMenu companies={companies} location={location} />
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  );
}
