"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 {
  AddProjectCodeButton,
  EditProjectCodeButton,
  type ProjectCodeRow,
} from "./project-code-form";
import { deleteProjectCode, toggleProjectCodeActive } from "./actions";

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

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

      <EditProjectCodeButton projectCode={projectCode} open={editOpen} onOpenChange={setEditOpen} />
      <DeleteConfirmDialog
        open={deleteOpen}
        onOpenChange={setDeleteOpen}
        label={projectCode.code}
        onConfirm={() => deleteProjectCode(projectCode.id)}
      />
      <ConfirmDialog
        open={toggleOpen}
        onOpenChange={setToggleOpen}
        title={projectCode.isActive ? "Deactivate project code?" : "Activate project code?"}
        description={
          projectCode.isActive
            ? "It will no longer appear in the Project Code dropdown."
            : "It will appear in the Project Code dropdown again."
        }
        confirmLabel={projectCode.isActive ? "Deactivate" : "Activate"}
        onConfirm={() => toggleProjectCodeActive(projectCode.id)}
      />
    </>
  );
}

export function ProjectCodesTable({ projectCodes }: { projectCodes: ProjectCodeRow[] }) {
  const { search, setSearch, sortKey, sortDir, toggleSort, activeFilters, toggleFilter, filtered } =
    useTableControls<ProjectCodeRow>({
      rows: projectCodes,
      defaultSortKey: "code",
      searchText: (c) => [c.code, c.isActive ? "active" : "inactive"].join(" "),
      chipMatch: (c, chip) => {
        if (chip.toLowerCase() === "active") return c.isActive;
        if (chip.toLowerCase() === "inactive") return !c.isActive;
        return false;
      },
      sortValue: (c, key) => {
        if (key === "isActive") return c.isActive ? "Active" : "Inactive";
        const val = c[key as keyof ProjectCodeRow];
        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">Project Codes</h1>
          <p className="text-sm text-neutral-500 mt-0.5">Codes that populate the PO &ldquo;Project Code&rdquo; dropdown</p>
        </div>
        <AddProjectCodeButton />
      </div>

      <TableControls
        search={search}
        onSearch={setSearch}
        searchPlaceholder="Search project code…"
        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 ProjectCodeRow)}>Project Code</SortableTh>
              <SortableTh sortKey="isActive" activeSortKey={sortKey as string | null} sortDir={sortDir} onSort={(k) => toggleSort(k as keyof ProjectCodeRow)}>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={3} className="px-4 py-8 text-center text-neutral-400">
                  No project codes yet. Add one to populate the Project Code dropdown.
                </td>
              </tr>
            )}
            {filtered.map((projectCode) => (
              <tr key={projectCode.id} className="hover:bg-neutral-50">
                <td className="px-4 py-3 font-medium text-neutral-900">{projectCode.code}</td>
                <td className="px-4 py-3">
                  <span className={`rounded-full px-2.5 py-0.5 text-xs font-medium ${
                    projectCode.isActive ? "bg-success-100 text-success-700" : "bg-neutral-100 text-neutral-500"
                  }`}>
                    {projectCode.isActive ? "Active" : "Inactive"}
                  </span>
                </td>
                <td className="px-4 py-3">
                  <ProjectCodeActionsMenu projectCode={projectCode} />
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  );
}
