"use client";

import React, { useState } from "react";
import Link from "next/link";
import {
  Search,
  RotateCcw,
  Plus,
  Eye,
  Edit2,
  Trash2,
  ChevronRight,
  FileSpreadsheet,
  UserCog,
  BookOpen,
  X,
  Save,
} from "lucide-react";
import { toast } from "sonner";

interface ExtendedAllocation {
  id: string;
  facultyName: string;
  facultyDesignation: string;
  department: string;
  subjectCode: string;
  subjectName: string;
  subjectType: "Degree" | "Diploma" | "Certificate";
  batchName: string;
  weeklyHours: number;
  status: "Active" | "Inactive";
}

const initialAllocations: ExtendedAllocation[] = [
  {
    id: "alloc-1",
    facultyName: "Chef Rajesh Sharma",
    facultyDesignation: "Senior Lecturer & HOD",
    department: "Food Production",
    subjectCode: "BHM201",
    subjectName: "Food Production Operations Practical",
    subjectType: "Degree",
    batchName: "Batch B1 (Kitchen Lab A)",
    weeklyHours: 4.0,
    status: "Active",
  },
  {
    id: "alloc-2",
    facultyName: "Chef Amit Roy",
    facultyDesignation: "Lecturer (Culinary Arts)",
    department: "Food Production",
    subjectCode: "BHM201",
    subjectName: "Food Production Operations Practical",
    subjectType: "Degree",
    batchName: "Batch B2 (Kitchen Lab B)",
    weeklyHours: 4.0,
    status: "Active",
  },
  {
    id: "alloc-3",
    facultyName: "Prof. Ananya Sen",
    facultyDesignation: "Assistant Professor & HOD",
    department: "F&B Service",
    subjectCode: "BHM202",
    subjectName: "Food & Beverage Service Theory",
    subjectType: "Degree",
    batchName: "Entire Semester III Cohort",
    weeklyHours: 2.0,
    status: "Active",
  },
  {
    id: "alloc-4",
    facultyName: "Chef Vikram Singh",
    facultyDesignation: "Master Baker & Lecturer",
    department: "Food Production",
    subjectCode: "BHM203",
    subjectName: "Bakery & Confectionery Practical",
    subjectType: "Degree",
    batchName: "Batch B1 (Bakery Lab 1)",
    weeklyHours: 4.0,
    status: "Active",
  },
  {
    id: "alloc-5",
    facultyName: "Prof. Priya Nair",
    facultyDesignation: "Senior Lecturer",
    department: "Front Office",
    subjectCode: "BHM204",
    subjectName: "Front Office Operations Theory",
    subjectType: "Degree",
    batchName: "Entire Semester III Cohort",
    weeklyHours: 2.0,
    status: "Active",
  },
  {
    id: "alloc-6",
    facultyName: "Chef Debashis Paul",
    facultyDesignation: "Lecturer",
    department: "Food Production",
    subjectCode: "DCM101",
    subjectName: "Culinary Techniques & Cuts",
    subjectType: "Diploma",
    batchName: "Batch D1 (Basic Kitchen)",
    weeklyHours: 6.0,
    status: "Active",
  },
  {
    id: "alloc-7",
    facultyName: "Ms. Moumita Bose",
    facultyDesignation: "Assistant Lecturer",
    department: "Housekeeping",
    subjectCode: "BHM205",
    subjectName: "Accommodation Operations Practical",
    subjectType: "Degree",
    batchName: "Batch B2 (Model Suite)",
    weeklyHours: 3.0,
    status: "Inactive",
  },
];

export default function FacultyAllocationPage() {
  const [allocations, setAllocations] = useState<ExtendedAllocation[]>(initialAllocations);
  const [selectedDept, setSelectedDept] = useState("All Departments");
  const [selectedType, setSelectedType] = useState("All Types");
  const [selectedStatus, setSelectedStatus] = useState("All Status");
  const [searchQuery, setSearchQuery] = useState("");
  const [selectedRows, setSelectedRows] = useState<string[]>([]);
  const [pageSize, setPageSize] = useState("10");

  // Modal State
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [modalMode, setModalMode] = useState<"create" | "edit" | "view">("create");
  const [editingId, setEditingId] = useState<string | null>(null);
  const [viewingAlloc, setViewingAlloc] = useState<ExtendedAllocation | null>(null);

  // Form State
  const [formData, setFormData] = useState({
    facultyName: "",
    facultyDesignation: "Lecturer",
    department: "Food Production",
    subjectCode: "",
    subjectName: "",
    subjectType: "Degree" as "Degree" | "Diploma" | "Certificate",
    batchName: "Batch B1",
    weeklyHours: 4.0,
    status: "Active" as "Active" | "Inactive",
  });

  const filteredAllocations = allocations.filter((a) => {
    const matchesDept =
      selectedDept === "All Departments" || a.department === selectedDept;
    const matchesType =
      selectedType === "All Types" || a.subjectType === selectedType;
    const matchesStatus =
      selectedStatus === "All Status" || a.status === selectedStatus;
    const matchesSearch =
      a.facultyName.toLowerCase().includes(searchQuery.toLowerCase()) ||
      a.subjectName.toLowerCase().includes(searchQuery.toLowerCase()) ||
      a.subjectCode.toLowerCase().includes(searchQuery.toLowerCase()) ||
      a.batchName.toLowerCase().includes(searchQuery.toLowerCase());
    return matchesDept && matchesType && matchesStatus && matchesSearch;
  });

  const handleResetFilters = () => {
    setSelectedDept("All Departments");
    setSelectedType("All Types");
    setSelectedStatus("All Status");
    setSearchQuery("");
  };

  const handleSelectAll = (e: React.ChangeEvent<HTMLInputElement>) => {
    if (e.target.checked) {
      setSelectedRows(filteredAllocations.map((a) => a.id));
    } else {
      setSelectedRows([]);
    }
  };

  const handleSelectRow = (id: string) => {
    if (selectedRows.includes(id)) {
      setSelectedRows(selectedRows.filter((rId) => rId !== id));
    } else {
      setSelectedRows([...selectedRows, id]);
    }
  };

  const handleDelete = (id: string) => {
    const target = allocations.find((a) => a.id === id);
    if (target?.status === "Active") {
      toast.error("Cannot delete active faculty allocation without reassigning.");
      return;
    }
    setAllocations(allocations.filter((a) => a.id !== id));
    toast.success("Allocation deleted successfully.");
  };

  const openCreateModal = () => {
    setModalMode("create");
    setEditingId(null);
    setViewingAlloc(null);
    setFormData({
      facultyName: "",
      facultyDesignation: "Lecturer",
      department: "Food Production",
      subjectCode: "",
      subjectName: "",
      subjectType: "Degree",
      batchName: "Batch B1",
      weeklyHours: 4.0,
      status: "Active",
    });
    setIsModalOpen(true);
  };

  const openEditModal = (a: ExtendedAllocation) => {
    setModalMode("edit");
    setEditingId(a.id);
    setViewingAlloc(null);
    setFormData({
      facultyName: a.facultyName,
      facultyDesignation: a.facultyDesignation,
      department: a.department,
      subjectCode: a.subjectCode,
      subjectName: a.subjectName,
      subjectType: a.subjectType,
      batchName: a.batchName,
      weeklyHours: a.weeklyHours,
      status: a.status,
    });
    setIsModalOpen(true);
  };

  const openViewModal = (a: ExtendedAllocation) => {
    setViewingAlloc(a);
    setModalMode("view");
    setIsModalOpen(true);
  };

  const handleSubmitForm = (e: React.FormEvent) => {
    e.preventDefault();

    if (!formData.facultyName.trim() || !formData.subjectCode.trim()) {
      toast.error("Please fill in faculty member and subject code.");
      return;
    }

    if (editingId) {
      setAllocations((prev) =>
        prev.map((a) =>
          a.id === editingId
            ? {
                ...a,
                facultyName: formData.facultyName,
                facultyDesignation: formData.facultyDesignation,
                department: formData.department,
                subjectCode: formData.subjectCode.toUpperCase(),
                subjectName: formData.subjectName,
                subjectType: formData.subjectType,
                batchName: formData.batchName,
                weeklyHours: formData.weeklyHours,
                status: formData.status,
              }
            : a
        )
      );
      toast.success("Faculty allocation updated successfully.");
    } else {
      const newAlloc: ExtendedAllocation = {
        id: `alloc-${Date.now()}`,
        facultyName: formData.facultyName,
        facultyDesignation: formData.facultyDesignation,
        department: formData.department,
        subjectCode: formData.subjectCode.toUpperCase(),
        subjectName: formData.subjectName,
        subjectType: formData.subjectType,
        batchName: formData.batchName,
        weeklyHours: formData.weeklyHours,
        status: formData.status,
      };
      setAllocations((prev) => [newAlloc, ...prev]);
      toast.success("New faculty teaching allocation created.");
    }

    setIsModalOpen(false);
  };

  return (
    <div className="space-y-5 font-sans">
      {/* 1. Header & Breadcrumbs */}
      <div className="flex flex-col lg:flex-row lg:items-center justify-between gap-4">
        <div>
          <nav className="flex items-center gap-1.5 text-[13px] text-[#64748B] mb-1 font-medium">
            <Link href="/admin/dashboard" className="hover:text-[#1769E8] hover:underline">
              Home
            </Link>
            <ChevronRight className="w-3.5 h-3.5 text-[#94A3B8]" />
            <Link href="/admin/people/faculty" className="hover:text-[#1769E8] hover:underline">
              Faculty Management
            </Link>
            <ChevronRight className="w-3.5 h-3.5 text-[#94A3B8]" />
            <span className="text-[#071A4D] font-semibold">Faculty Allocation</span>
          </nav>

          <h1 className="text-3xl font-bold font-heading text-[#071A4D] tracking-tight">
            Faculty Allocation
          </h1>
          <p className="text-[15px] text-[#64748B] mt-1 font-normal">
            Map faculty instructors to subjects, theoretical classrooms, and practical batches.
          </p>
        </div>

        <div className="flex items-center gap-2.5 flex-wrap">
          <button
            onClick={() => toast.success("Exporting Faculty Allocation directory...")}
            className="inline-flex items-center gap-1.5 px-3.5 py-2 text-xs font-semibold text-[#071A4D] bg-white border border-[#E5EAF0] rounded-lg hover:bg-slate-50 transition-colors shadow-2xs cursor-pointer"
          >
            <FileSpreadsheet className="w-4 h-4 text-emerald-600" />
            <span>Export</span>
          </button>

          <button
            onClick={openCreateModal}
            className="inline-flex items-center gap-1.5 px-4 py-2 text-xs font-semibold text-white bg-[#1769E8] hover:bg-[#1257C7] rounded-lg shadow-sm transition-colors cursor-pointer"
          >
            <Plus className="w-4 h-4" />
            <span>+ New Allocation</span>
          </button>
        </div>
      </div>

      {/* 2. Filter Bar */}
      <div className="p-4 rounded-xl bg-white border border-[#E5EAF0] shadow-xs flex flex-col lg:flex-row items-stretch lg:items-center justify-between gap-3">
        <div className="flex flex-wrap items-center gap-2.5 flex-1">
          <div className="flex flex-col">
            <label className="text-[10px] font-bold text-[#64748B] mb-0.5 uppercase tracking-wider">
              Department
            </label>
            <select
              value={selectedDept}
              onChange={(e) => setSelectedDept(e.target.value)}
              className="h-9 px-3 rounded-lg border border-[#D8E0EA] bg-white text-xs font-medium text-[#334155] focus:outline-none focus:border-[#1769E8] cursor-pointer"
            >
              <option>All Departments</option>
              <option value="Food Production">Food Production</option>
              <option value="F&B Service">F&B Service</option>
              <option value="Front Office">Front Office</option>
              <option value="Housekeeping">Housekeeping</option>
            </select>
          </div>

          <div className="flex flex-col">
            <label className="text-[10px] font-bold text-[#64748B] mb-0.5 uppercase tracking-wider">
              Program Type
            </label>
            <select
              value={selectedType}
              onChange={(e) => setSelectedType(e.target.value)}
              className="h-9 px-3 rounded-lg border border-[#D8E0EA] bg-white text-xs font-medium text-[#334155] focus:outline-none focus:border-[#1769E8] cursor-pointer"
            >
              <option>All Types</option>
              <option value="Degree">Degree</option>
              <option value="Diploma">Diploma</option>
              <option value="Certificate">Certificate</option>
            </select>
          </div>

          <div className="flex flex-col">
            <label className="text-[10px] font-bold text-[#64748B] mb-0.5 uppercase tracking-wider">
              Status
            </label>
            <select
              value={selectedStatus}
              onChange={(e) => setSelectedStatus(e.target.value)}
              className="h-9 px-3 rounded-lg border border-[#D8E0EA] bg-white text-xs font-medium text-[#334155] focus:outline-none focus:border-[#1769E8] cursor-pointer"
            >
              <option>All Status</option>
              <option value="Active">Active</option>
              <option value="Inactive">Inactive</option>
            </select>
          </div>

          <div className="flex flex-col flex-1 min-w-[220px]">
            <label className="text-[10px] font-bold text-[#64748B] mb-0.5 uppercase tracking-wider opacity-0">
              Search
            </label>
            <div className="relative">
              <Search className="w-4 h-4 text-[#94A3B8] absolute left-3 top-1/2 -translate-y-1/2" />
              <input
                type="text"
                value={searchQuery}
                onChange={(e) => setSearchQuery(e.target.value)}
                placeholder="Search by faculty, subject, or batch..."
                className="h-9 w-full rounded-lg border border-[#D8E0EA] bg-white pl-9 pr-3 text-xs text-[#334155] placeholder:text-[#94A3B8] focus:outline-none focus:border-[#1769E8]"
              />
            </div>
          </div>
        </div>

        <div className="flex items-end gap-2 shrink-0 pt-3 lg:pt-0">
          <button
            onClick={() => {}}
            className="h-9 px-4 rounded-lg bg-[#1769E8] hover:bg-[#1257C7] text-white text-xs font-semibold flex items-center gap-1.5 shadow-xs transition-colors cursor-pointer"
          >
            <Search className="w-3.5 h-3.5" />
            <span>Search</span>
          </button>

          <button
            onClick={handleResetFilters}
            className="h-9 px-4 rounded-lg border border-[#E5EAF0] hover:bg-slate-50 text-[#334155] text-xs font-semibold flex items-center gap-1.5 transition-colors shadow-2xs cursor-pointer"
          >
            <RotateCcw className="w-3.5 h-3.5 text-[#64748B]" />
            <span>Reset</span>
          </button>
        </div>
      </div>

      {/* 3. Full Width Table - All Details in a Line */}
      <div className="rounded-xl bg-white border border-[#E5EAF0] shadow-xs overflow-hidden">
        <div className="p-4 border-b border-[#E5EAF0] flex items-center justify-between">
          <div className="flex items-center gap-2">
            <UserCog className="w-4 h-4 text-[#1769E8]" />
            <h2 className="text-sm font-bold font-heading text-[#071A4D]">
              Faculty Allocations ({filteredAllocations.length})
            </h2>
          </div>

          <select
            value={pageSize}
            onChange={(e) => setPageSize(e.target.value)}
            className="h-8 px-2.5 rounded-lg border border-[#D8E0EA] bg-white text-xs text-[#64748B] focus:outline-none focus:border-[#1769E8] cursor-pointer"
          >
            <option value="10">10 per page</option>
            <option value="25">25 per page</option>
            <option value="50">50 per page</option>
          </select>
        </div>

        <div className="overflow-x-auto">
          <table className="w-full text-left text-xs border-collapse">
            <thead>
              <tr className="bg-[#F8FAFC] border-b border-[#E5EAF0] text-[#64748B] uppercase tracking-wider font-bold text-[11px] whitespace-nowrap">
                <th className="py-3 px-3.5 text-center w-10">
                  <input
                    type="checkbox"
                    checked={selectedRows.length === filteredAllocations.length && filteredAllocations.length > 0}
                    onChange={handleSelectAll}
                    className="rounded border-[#D8E0EA] text-[#1769E8] focus:ring-[#1769E8]"
                  />
                </th>
                <th className="py-3 px-2 text-center w-10">#</th>
                <th className="py-3 px-4">Faculty Member</th>
                <th className="py-3 px-4">Department</th>
                <th className="py-3 px-4">Subject Code & Title</th>
                <th className="py-3 px-4">Type</th>
                <th className="py-3 px-4">Allocated Batch / Section</th>
                <th className="py-3 px-4 text-center">Weekly Hours</th>
                <th className="py-3 px-4 text-center">Status</th>
                <th className="py-3 px-4 text-center">Actions</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-[#E5EAF0] text-[#334155]">
              {filteredAllocations.length === 0 ? (
                <tr>
                  <td colSpan={10} className="py-12 text-center text-slate-400">
                    No faculty allocations match the search criteria.
                  </td>
                </tr>
              ) : (
                filteredAllocations.map((a, idx) => {
                  const isSelected = selectedRows.includes(a.id);
                  return (
                    <tr
                      key={a.id}
                      className={`hover:bg-[#F8FAFC] transition-colors whitespace-nowrap ${
                        isSelected ? "bg-blue-50/40" : ""
                      }`}
                    >
                      <td className="py-3.5 px-3.5 text-center">
                        <input
                          type="checkbox"
                          checked={isSelected}
                          onChange={() => handleSelectRow(a.id)}
                          className="rounded border-[#D8E0EA] text-[#1769E8] focus:ring-[#1769E8]"
                        />
                      </td>
                      <td className="py-3.5 px-2 text-center text-slate-400 font-medium">
                        {idx + 1}
                      </td>
                      <td className="py-3.5 px-4">
                        <div>
                          <span className="font-semibold text-[#071A4D] block">{a.facultyName}</span>
                          <span className="text-[10px] text-[#64748B] block">{a.facultyDesignation}</span>
                        </div>
                      </td>
                      <td className="py-3.5 px-4">
                        <span className="inline-flex items-center px-2 py-0.5 rounded-md text-[11px] font-medium bg-slate-100 text-slate-700">
                          {a.department}
                        </span>
                      </td>
                      <td className="py-3.5 px-4">
                        <div className="flex items-center gap-1.5">
                          <span className="font-mono bg-blue-50 text-[#1769E8] px-1.5 py-0.5 rounded text-[11px] font-bold">
                            {a.subjectCode}
                          </span>
                          <span className="text-[#334155] font-medium max-w-xs truncate">{a.subjectName}</span>
                        </div>
                      </td>
                      <td className="py-3.5 px-4 text-[#475569]">
                        {a.subjectType}
                      </td>
                      <td className="py-3.5 px-4 text-[#334155]">
                        {a.batchName}
                      </td>
                      <td className="py-3.5 px-4 text-center font-bold text-[#071A4D]">
                        {a.weeklyHours} hrs/wk
                      </td>
                      <td className="py-3.5 px-4 text-center">
                        {a.status === "Active" ? (
                          <span className="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-[11px] font-semibold bg-[#DCFCE7] text-[#15803D]">
                            <span className="w-1.5 h-1.5 rounded-full bg-[#15803D]"></span>
                            Active
                          </span>
                        ) : (
                          <span className="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-[11px] font-semibold bg-[#E2E8F0] text-[#475569]">
                            <span className="w-1.5 h-1.5 rounded-full bg-[#475569]"></span>
                            Inactive
                          </span>
                        )}
                      </td>
                      <td className="py-3.5 px-4 text-center">
                        <div className="flex items-center justify-center gap-1.5">
                          <button
                            onClick={() => openViewModal(a)}
                            className="p-1.5 rounded-lg text-[#1769E8] hover:bg-blue-50 transition-colors"
                            title="View allocation details"
                          >
                            <Eye className="w-4 h-4" />
                          </button>

                          <button
                            onClick={() => openEditModal(a)}
                            className="p-1.5 rounded-lg text-[#1769E8] hover:bg-blue-50 transition-colors"
                            title="Edit allocation"
                          >
                            <Edit2 className="w-4 h-4" />
                          </button>

                          <button
                            onClick={() => handleDelete(a.id)}
                            className="p-1.5 rounded-lg text-[#DC2626] hover:bg-red-50 transition-colors"
                            title="Delete allocation"
                          >
                            <Trash2 className="w-4 h-4" />
                          </button>
                        </div>
                      </td>
                    </tr>
                  );
                })
              )}
            </tbody>
          </table>
        </div>

        {/* Pagination Footer */}
        <div className="p-3.5 px-4 border-t border-[#E5EAF0] flex items-center justify-between text-xs text-[#64748B]">
          <div>
            Showing 1 to {filteredAllocations.length} of {allocations.length} allocations
          </div>
          <div className="flex items-center gap-1">
            <button
              disabled
              className="px-2.5 py-1 rounded border border-[#E5EAF0] text-slate-300 cursor-not-allowed"
            >
              &lt;
            </button>
            <button className="px-3 py-1 rounded bg-[#1769E8] text-white font-bold">
              1
            </button>
            <button
              disabled
              className="px-2.5 py-1 rounded border border-[#E5EAF0] text-slate-300 cursor-not-allowed"
            >
              &gt;
            </button>
          </div>
        </div>
      </div>

      {/* 4. POPUP MODAL: CREATE / EDIT / VIEW ALLOCATION */}
      {isModalOpen && (
        <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-xs p-4 animate-in fade-in duration-150">
          <div className="w-full max-w-lg bg-white rounded-2xl shadow-2xl border border-[#E5EAF0] overflow-hidden flex flex-col max-h-[90vh]">
            <div className="px-6 py-4 border-b border-[#E5EAF0] flex items-center justify-between bg-slate-50/60">
              <div className="flex items-center gap-2.5">
                <div className="w-9 h-9 rounded-xl bg-blue-50 text-[#1769E8] flex items-center justify-center">
                  <UserCog className="w-5 h-5" />
                </div>
                <div>
                  <h3 className="font-heading font-bold text-base text-[#071A4D]">
                    {modalMode === "create" && "New Faculty Allocation"}
                    {modalMode === "edit" && "Edit Faculty Allocation"}
                    {modalMode === "view" && "Allocation Details"}
                  </h3>
                  <p className="text-[12px] text-[#64748B]">
                    {modalMode === "create" && "Assign a teacher to a subject course or batch."}
                    {modalMode === "edit" && "Modify teaching hours or assigned batches."}
                    {modalMode === "view" && "Overview of this faculty teaching assignment."}
                  </p>
                </div>
              </div>
              <button
                onClick={() => setIsModalOpen(false)}
                className="w-8 h-8 rounded-lg text-slate-400 hover:text-slate-700 hover:bg-slate-100 flex items-center justify-center transition-colors cursor-pointer"
              >
                <X className="w-4 h-4" />
              </button>
            </div>

            <div className="p-6 overflow-y-auto space-y-4">
              {modalMode === "view" && viewingAlloc ? (
                <div className="space-y-4 text-xs">
                  <div className="grid grid-cols-2 gap-4 p-4 rounded-xl bg-slate-50 border border-slate-100">
                    <div>
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Faculty Member</span>
                      <span className="text-sm font-bold text-[#071A4D]">{viewingAlloc.facultyName}</span>
                      <span className="text-[11px] text-[#64748B] block">{viewingAlloc.facultyDesignation}</span>
                    </div>
                    <div>
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Department</span>
                      <span className="text-sm font-semibold text-[#071A4D]">{viewingAlloc.department}</span>
                    </div>
                    <div className="col-span-2">
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Assigned Subject</span>
                      <div className="flex items-center gap-1.5 mt-0.5">
                        <span className="font-mono bg-blue-50 text-[#1769E8] px-1.5 py-0.5 rounded text-[11px] font-bold">
                          {viewingAlloc.subjectCode}
                        </span>
                        <span className="text-xs font-medium text-[#334155]">{viewingAlloc.subjectName}</span>
                      </div>
                    </div>
                    <div>
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Batch / Section</span>
                      <span className="text-xs font-medium text-[#334155]">{viewingAlloc.batchName}</span>
                    </div>
                    <div>
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Program Type</span>
                      <span className="text-xs font-medium text-[#334155]">{viewingAlloc.subjectType}</span>
                    </div>
                    <div>
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Weekly Load</span>
                      <span className="text-xs font-bold text-[#071A4D]">{viewingAlloc.weeklyHours} Hours / Week</span>
                    </div>
                    <div>
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Status</span>
                      <span className={`inline-block mt-0.5 px-2 py-0.5 rounded-full font-semibold text-[10px] ${
                        viewingAlloc.status === "Active"
                          ? "bg-emerald-100 text-emerald-700"
                          : "bg-slate-200 text-slate-700"
                      }`}>
                        {viewingAlloc.status}
                      </span>
                    </div>
                  </div>
                </div>
              ) : (
                <form id="alloc-form" onSubmit={handleSubmitForm} className="space-y-4">
                  <div className="grid grid-cols-2 gap-3">
                    <div>
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Faculty Name <span className="text-[#DC2626]">*</span>
                      </label>
                      <input
                        type="text"
                        required
                        value={formData.facultyName}
                        onChange={(e) => setFormData({ ...formData, facultyName: e.target.value })}
                        placeholder="e.g. Chef Rajesh Sharma"
                        className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] placeholder:text-[#94A3B8] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20"
                      />
                    </div>
                    <div>
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Designation
                      </label>
                      <input
                        type="text"
                        value={formData.facultyDesignation}
                        onChange={(e) => setFormData({ ...formData, facultyDesignation: e.target.value })}
                        placeholder="e.g. Senior Lecturer"
                        className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20"
                      />
                    </div>
                  </div>

                  <div className="grid grid-cols-2 gap-3">
                    <div>
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Department
                      </label>
                      <select
                        value={formData.department}
                        onChange={(e) => setFormData({ ...formData, department: e.target.value })}
                        className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20 cursor-pointer"
                      >
                        <option value="Food Production">Food Production</option>
                        <option value="F&B Service">F&B Service</option>
                        <option value="Front Office">Front Office</option>
                        <option value="Housekeeping">Housekeeping</option>
                      </select>
                    </div>
                    <div>
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Program Type
                      </label>
                      <select
                        value={formData.subjectType}
                        onChange={(e) =>
                          setFormData({
                            ...formData,
                            subjectType: e.target.value as ExtendedAllocation["subjectType"],
                          })
                        }
                        className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20 cursor-pointer"
                      >
                        <option value="Degree">Degree</option>
                        <option value="Diploma">Diploma</option>
                        <option value="Certificate">Certificate</option>
                      </select>
                    </div>
                  </div>

                  <div className="grid grid-cols-1 sm:grid-cols-3 gap-3">
                    <div>
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Subject Code <span className="text-[#DC2626]">*</span>
                      </label>
                      <input
                        type="text"
                        required
                        value={formData.subjectCode}
                        onChange={(e) => setFormData({ ...formData, subjectCode: e.target.value })}
                        placeholder="e.g. BHM201"
                        className="h-10 w-full px-3 text-xs uppercase font-mono rounded-lg border border-[#D8E0EA] bg-white text-[#334155] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20"
                      />
                    </div>
                    <div className="sm:col-span-2">
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Subject Title
                      </label>
                      <input
                        type="text"
                        value={formData.subjectName}
                        onChange={(e) => setFormData({ ...formData, subjectName: e.target.value })}
                        placeholder="e.g. Food Production Operations"
                        className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20"
                      />
                    </div>
                  </div>

                  <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
                    <div>
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Batch / Section Name
                      </label>
                      <input
                        type="text"
                        value={formData.batchName}
                        onChange={(e) => setFormData({ ...formData, batchName: e.target.value })}
                        placeholder="e.g. Batch B1 (Kitchen Lab A)"
                        className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20"
                      />
                    </div>
                    <div>
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Weekly Hours
                      </label>
                      <input
                        type="number"
                        step="0.5"
                        min="1"
                        max="30"
                        value={formData.weeklyHours}
                        onChange={(e) => setFormData({ ...formData, weeklyHours: Number(e.target.value) })}
                        className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20"
                      />
                    </div>
                  </div>

                  <div>
                    <label className="block text-xs font-bold text-[#334155] mb-2">
                      Status
                    </label>
                    <div className="flex items-center gap-5 text-xs">
                      <label className="flex items-center gap-1.5 cursor-pointer text-[#334155]">
                        <input
                          type="radio"
                          name="allocStatus"
                          checked={formData.status === "Active"}
                          onChange={() => setFormData({ ...formData, status: "Active" })}
                          className="text-[#1769E8] focus:ring-[#1769E8]"
                        />
                        <span className="font-medium">Active</span>
                      </label>

                      <label className="flex items-center gap-1.5 cursor-pointer text-[#334155]">
                        <input
                          type="radio"
                          name="allocStatus"
                          checked={formData.status === "Inactive"}
                          onChange={() => setFormData({ ...formData, status: "Inactive" })}
                          className="text-[#1769E8] focus:ring-[#1769E8]"
                        />
                        <span className="font-medium">Inactive</span>
                      </label>
                    </div>
                  </div>
                </form>
              )}
            </div>

            <div className="px-6 py-3.5 border-t border-[#E5EAF0] flex items-center justify-end gap-2.5 bg-slate-50/50">
              <button
                type="button"
                onClick={() => setIsModalOpen(false)}
                className="px-4 py-2 text-xs font-semibold text-[#64748B] bg-white hover:bg-slate-100 border border-[#E5EAF0] rounded-lg transition-colors cursor-pointer"
              >
                {modalMode === "view" ? "Close" : "Cancel"}
              </button>

              {modalMode !== "view" && (
                <button
                  type="submit"
                  form="alloc-form"
                  className="px-5 py-2 text-xs font-bold text-white bg-[#1769E8] hover:bg-[#1257C7] rounded-lg transition-colors flex items-center gap-1.5 shadow-xs cursor-pointer"
                >
                  <Save className="w-3.5 h-3.5" />
                  <span>{modalMode === "create" ? "Create Allocation" : "Save Changes"}</span>
                </button>
              )}
            </div>
          </div>
        </div>
      )}
    </div>
  );
}
