"use client";

import placeHolderImage from "@/assets/images/avatar.jpeg";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { cn } from "@/lib/utils";
import { ArrowLeft, Briefcase, CalendarDays, ChevronDown, Eye, MoreVertical, Search, User, X } from "lucide-react";
import Image from "next/image";
import Link from "next/link";
import { useMemo, useState } from "react";

type Status = "Permanent" | "Intern";
type WorkType = "Office" | "Remote";

type Employee = {
  id: string;
  name: string;
  employeeId: string;
  designation: string;
  type: WorkType;
  status: Status;
  image: string;
};

const SEED_EMPLOYEES: Employee[] = [
  { id: "1", name: "Jacob Jones", employeeId: "15f3ad1", designation: "Finance Minister", type: "Office", status: "Permanent", image: placeHolderImage.src },
  { id: "2", name: "Leaise Watson", employeeId: "f1d53f9", designation: "Finance Secretary", type: "Remote", status: "Intern", image: placeHolderImage.src },
  { id: "3", name: "Leslie Alexander", employeeId: "d9er85d", designation: "PS to Finance secretary", type: "Office", status: "Permanent", image: placeHolderImage.src },
  { id: "4", name: "Christian Dior", employeeId: "5d7fe74", designation: "Special Secretary (A&R)", type: "Remote", status: "Permanent", image: placeHolderImage.src },
  { id: "5", name: "Jenny Wilson", employeeId: "f46a5f3", designation: "Consultant KP FM", type: "Office", status: "Intern", image: placeHolderImage.src },
  { id: "6", name: "Ronald Richard", employeeId: "85f2e6a", designation: "Consultant PFC", type: "Office", status: "Permanent", image: placeHolderImage.src },
];

function ModalShell({
  open,
  onClose,
  children,
  width = "max-w-[640px]",
}: {
  open: boolean;
  onClose: () => void;
  children: React.ReactNode;
  width?: string;
}) {
  if (!open) return null;
  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/35 backdrop-blur-[2px] p-4">
      <div className={cn("w-full rounded-xl bg-white p-5 shadow-2xl", width)}>
        <div className="mb-4 flex items-start justify-between">
          <span className="inline-flex size-10 items-center justify-center rounded-lg border border-[#E7ECF2] text-[#0B1727]">
            <User className="size-4" />
          </span>
          <button
            type="button"
            onClick={onClose}
            className="inline-flex size-8 items-center justify-center rounded-lg bg-[#EEF5FB] text-[#5E6B7A]"
          >
            <X className="size-4" />
          </button>
        </div>
        {children}
      </div>
    </div>
  );
}

export default function Page({ params }: { params: { app: string; category: string } }) {
  const { app, category } = params;
  const title = `${category.charAt(0).toUpperCase()}${category.slice(1)} Department Information`;
  const [search, setSearch] = useState("");
  const [rows, setRows] = useState<Employee[]>(SEED_EMPLOYEES);
  const [openAdd, setOpenAdd] = useState(false);
  const [openView, setOpenView] = useState(false);
  const [openDelete, setOpenDelete] = useState(false);
  const [active, setActive] = useState<Employee | null>(null);

  const filtered = useMemo(() => {
    const q = search.trim().toLowerCase();
    if (!q) return rows;
    return rows.filter((r) =>
      [r.name, r.employeeId, r.designation, r.type, r.status].some((f) => f.toLowerCase().includes(q)),
    );
  }, [rows, search]);

  return (
    <div className="space-y-4">
      <div className="mb-2 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
        <div className="flex items-center gap-2.5">
          <Link href={`/${app}/departments`} className="rounded bg-white p-2 text-[#383E49]">
            <ArrowLeft className="size-4" />
          </Link>
          <h1 className="text-[30px] font-semibold leading-none text-[#272833]">{title}</h1>
        </div>
        <Button className="h-9 rounded bg-[#074473] px-5 text-[12px] font-semibold text-white hover:bg-[#074473]/90" onClick={() => setOpenAdd(true)}>
          Add New Employee
        </Button>
      </div>

      <Card className="border-[#EBEEF2]">
        <CardContent className="space-y-3 p-4">
          <div className="flex flex-wrap items-center justify-between gap-2">
            <h2 className="text-[27px] font-semibold text-[#272833]">{category.charAt(0).toUpperCase() + category.slice(1)} Department</h2>
            <div className="flex items-center gap-2">
              <div className="relative">
                <Search className="absolute left-2 top-1/2 size-3.5 -translate-y-1/2 text-[#9AA1AB]" />
                <Input className="h-8 w-[170px] bg-white pl-7 text-[11px]" placeholder="Search" value={search} onChange={(e) => setSearch(e.target.value)} />
              </div>
              <Button variant="outline" className="h-8 gap-1 bg-white px-3 text-[11px]"><Briefcase className="size-3.5" /> Filters</Button>
              <Button variant="outline" className="h-8 gap-1 bg-white px-3 text-[11px]"><CalendarDays className="size-3.5" /> filters</Button>
            </div>
          </div>

          <div className="overflow-x-auto rounded-lg border border-[#EBEEF2]">
            <table className="min-w-[980px] w-full border-collapse">
              <thead>
                <tr className="bg-[#FAFBFD] text-left">
                  <th className="px-4 py-3 text-[12px] font-semibold text-[#272833]"><input type="checkbox" /></th>
                  <th className="px-3 py-3 text-[12px] font-semibold text-[#272833]">Image</th>
                  <th className="px-3 py-3 text-[12px] font-semibold text-[#272833]">Name</th>
                  <th className="px-3 py-3 text-[12px] font-semibold text-[#272833]">Employee ID</th>
                  <th className="px-3 py-3 text-[12px] font-semibold text-[#272833]">Designation</th>
                  <th className="px-3 py-3 text-[12px] font-semibold text-[#272833]">Type</th>
                  <th className="px-3 py-3 text-[12px] font-semibold text-[#272833]">Status</th>
                  <th className="px-3 py-3 text-[12px] font-semibold text-[#272833]">Action</th>
                </tr>
              </thead>
              <tbody>
                {filtered.map((row) => (
                  <tr key={row.id} className="border-t border-[#EEF1F4]">
                    <td className="px-4 py-2.5"><input type="checkbox" /></td>
                    <td className="px-3 py-2.5"><Image src={row.image} alt={row.name} width={34} height={34} className="size-[34px] rounded object-cover" /></td>
                    <td className="px-3 py-2.5 text-[12px] font-medium text-[#272833]">{row.name}</td>
                    <td className="px-3 py-2.5 text-[12px] text-[#272833]">{row.employeeId}</td>
                    <td className="px-3 py-2.5 text-[12px] text-[#272833]">{row.designation}</td>
                    <td className="px-3 py-2.5 text-[12px] text-[#272833]">{row.type}</td>
                    <td className="px-3 py-2.5">
                      <span className={cn("inline-flex h-7 min-w-[92px] items-center justify-between rounded border px-2 text-[11px] font-medium", row.status === "Permanent" ? "border-[#22C55E] text-[#22C55E]" : "border-[#EF4444] text-[#EF4444]")}>
                        {row.status}
                        <ChevronDown className="size-3.5" />
                      </span>
                    </td>
                    <td className="px-3 py-2.5">
                      <div className="flex items-center gap-2">
                        <button
                          type="button"
                          onClick={() => {
                            setActive(row);
                            setOpenView(true);
                          }}
                          className="inline-flex size-7 items-center justify-center rounded bg-[#EEF5FB] text-[#074473]"
                        >
                          <Eye className="size-3.5" />
                        </button>
                        <button
                          type="button"
                          onClick={() => {
                            setActive(row);
                            setOpenDelete(true);
                          }}
                          className="inline-flex size-7 items-center justify-center rounded bg-[#FEECEC] text-[#F04438]"
                        >
                          <MoreVertical className="size-3.5" />
                        </button>
                      </div>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>

          <div className="flex items-center justify-between">
            <Button variant="outline" className="h-8 bg-white px-4 text-[11px]">Previous</Button>
            <p className="text-[11px] text-[#8B8D97]">page 1 of 10</p>
            <Button variant="outline" className="h-8 bg-white px-4 text-[11px]">Next</Button>
          </div>
        </CardContent>
      </Card>

      <ModalShell open={openAdd} onClose={() => setOpenAdd(false)} width="max-w-[560px]">
        <h3 className="mb-3 text-[30px] font-semibold text-[#272833]">Add New Employee</h3>
        {["Employe Name", "Designation", "Type", "Status"].map((label) => (
          <div key={label} className="mb-2.5">
            <p className="mb-1 text-[12px] text-[#545D69]">{label}</p>
            <button className="flex h-10 w-full items-center justify-between rounded-md border border-[#EEF1F4] bg-[#F8F9FC] px-3 text-[13px] text-[#7A8490]">
              {label === "Employe Name" ? "Michael Jordon" : label === "Designation" ? "Finance Secretary" : label === "Type" ? "Office" : "Permanent"}
              <ChevronDown className="size-4" />
            </button>
          </div>
        ))}
        <div className="mt-3 flex items-center justify-center gap-3">
          <Button className="h-9 min-w-[110px] rounded bg-[#F79009] text-[12px] font-semibold text-white" onClick={() => setOpenAdd(false)}>Cancel</Button>
          <Button className="h-9 min-w-[110px] rounded bg-[#074473] text-[12px] font-semibold text-white" onClick={() => setOpenAdd(false)}>Add</Button>
        </div>
      </ModalShell>

      <ModalShell open={openView} onClose={() => setOpenView(false)} width="max-w-[860px]">
        <h3 className="mb-3 text-[30px] font-semibold text-[#272833]">View Employe Info</h3>
        <div className="overflow-x-auto rounded-lg border border-[#EBEEF2]">
          <table className="w-full min-w-[760px]">
            <thead>
              <tr className="bg-[#FAFBFD] text-left">
                {["Employee Name", "Employee ID", "Designation", "Type", "Status"].map((h) => (
                  <th key={h} className="px-3 py-2.5 text-[12px] font-semibold text-[#272833]">{h}</th>
                ))}
              </tr>
            </thead>
            <tbody>
              <tr className="border-t border-[#EEF1F4]">
                <td className="px-3 py-3 text-[12px] text-[#272833]">{active?.name ?? "Jacob Jones"}</td>
                <td className="px-3 py-3 text-[12px] text-[#272833]">{active?.employeeId ?? "15f3ad1"}</td>
                <td className="px-3 py-3 text-[12px] text-[#272833]">{active?.designation ?? "Finance Minister"}</td>
                <td className="px-3 py-3 text-[12px] text-[#272833]">{active?.type ?? "Office"}</td>
                <td className="px-3 py-3 text-[12px] text-[#272833]">{active?.status ?? "Permanent"}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </ModalShell>

      <ModalShell open={openDelete} onClose={() => setOpenDelete(false)} width="max-w-[760px]">
        <h3 className="mb-3 text-[30px] font-semibold text-[#272833]">Delete Employe From Department</h3>
        <div className="mb-4 flex items-center gap-3 rounded border border-[#E7ECF2] p-3">
          <Image src={placeHolderImage} alt="" width={48} height={48} className="size-12 rounded object-cover" />
          <div className="grid flex-1 grid-cols-3 gap-2 text-[12px]">
            <div>
              <p className="text-[#8B8D97]">{active?.name ?? "Jacob Jones"}</p>
              <p className="font-medium text-[#272833]">ID: {active?.employeeId ?? "15f3ad1"}</p>
            </div>
            <div>
              <p className="text-[#8B8D97]">Purchase date:</p>
              <p className="font-medium text-[#272833]">{active?.designation ?? "Finance Minister"}</p>
            </div>
            <div>
              <p className="text-[#8B8D97]">Type:</p>
              <p className="font-medium text-[#272833]">{active?.type ?? "Office"}</p>
            </div>
          </div>
        </div>
        <p className="mb-5 text-center text-[31px] font-semibold leading-tight text-[#272833]">
          Are You Sure You Want To Delete
          <br />
          {active?.name ?? "Jacob Jones"} From Finance Department?
        </p>
        <div className="flex items-center justify-center gap-3">
          <Button className="h-9 min-w-[110px] rounded bg-[#F79009] text-[12px] font-semibold text-white" onClick={() => setOpenDelete(false)}>Cancel</Button>
          <Button
            className="h-9 min-w-[110px] rounded bg-[#074473] text-[12px] font-semibold text-white"
            onClick={() => {
              if (!active) return;
              setRows((prev) => prev.filter((r) => r.id !== active.id));
              setOpenDelete(false);
            }}
          >
            Delete
          </Button>
        </div>
      </ModalShell>
    </div>
  );
}