"use client";

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

interface SubjectScheduleRecord {
  id: string;
  subjectCode: string;
  subjectTitle: string;
  theoryHours: number;
  practicalHours: number;
  totalWeeklyHours: number;
  facultyLead: string;
  semester: string;
}

const initialSubjectSchedules: SubjectScheduleRecord[] = [
  {
    id: "sub-1",
    subjectCode: "BHM201",
    subjectTitle: "Food Production Operations Practical",
    theoryHours: 0,
    practicalHours: 8,
    totalWeeklyHours: 8,
    facultyLead: "Chef Rajesh Sharma",
    semester: "Semester III",
  },
  {
    id: "sub-2",
    subjectCode: "BHM202",
    subjectTitle: "Food & Beverage Service Theory",
    theoryHours: 2,
    practicalHours: 0,
    totalWeeklyHours: 2,
    facultyLead: "Prof. Ananya Sen",
    semester: "Semester III",
  },
  {
    id: "sub-3",
    subjectCode: "BHM203",
    subjectTitle: "Bakery & Confectionery Practical",
    theoryHours: 0,
    practicalHours: 4,
    totalWeeklyHours: 4,
    facultyLead: "Chef Vikram Singh",
    semester: "Semester III",
  },
  {
    id: "sub-4",
    subjectCode: "BHM204",
    subjectTitle: "Front Office Operations Theory",
    theoryHours: 2,
    practicalHours: 0,
    totalWeeklyHours: 2,
    facultyLead: "Prof. Priya Nair",
    semester: "Semester III",
  },
  {
    id: "sub-5",
    subjectCode: "BHM205",
    subjectTitle: "Accommodation Operations Theory",
    theoryHours: 2,
    practicalHours: 0,
    totalWeeklyHours: 2,
    facultyLead: "Ms. Moumita Bose",
    semester: "Semester III",
  },
  {
    id: "sub-6",
    subjectCode: "BHM111",
    subjectTitle: "Foundation Culinary Skills & Stocks",
    theoryHours: 1,
    practicalHours: 6,
    totalWeeklyHours: 7,
    facultyLead: "Chef Amit Roy",
    semester: "Semester I",
  },
];

export default function SubjectwiseSchedulePage() {
  const [subjectSchedules, setSubjectSchedules] = useState<SubjectScheduleRecord[]>(initialSubjectSchedules);
  const [selectedSemester, setSelectedSemester] = useState("All Semesters");
  const [searchQuery, setSearchQuery] = useState("");
  const [selectedRows, setSelectedRows] = useState<string[]>([]);
  const [pageSize, setPageSize] = useState("10");

  // Modal State
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [viewingRecord, setViewingRecord] = useState<SubjectScheduleRecord | null>(null);
  const [editingId, setEditingId] = useState<string | null>(null);

  const [formData, setFormData] = useState({
    subjectCode: "BHM201",
    subjectTitle: "",
    theoryHours: 2,
    practicalHours: 4,
    facultyLead: "Chef Rajesh Sharma",
    semester: "Semester III",
  });

  const filteredRecords = subjectSchedules.filter((s) => {
    const matchesSem = selectedSemester === "All Semesters" || s.semester === selectedSemester;
    const matchesSearch =
      searchQuery === "" ||
      s.subjectCode.toLowerCase().includes(searchQuery.toLowerCase()) ||
      s.subjectTitle.toLowerCase().includes(searchQuery.toLowerCase()) ||
      s.facultyLead.toLowerCase().includes(searchQuery.toLowerCase()) ||
      s.semester.toLowerCase().includes(searchQuery.toLowerCase());
    return matchesSem && matchesSearch;
  });

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

  const handleRowSelect = (id: string) => {
    setSelectedRows((prev) =>
      prev.includes(id) ? prev.filter((r) => r !== id) : [...prev, id]
    );
  };

  const handleResetFilters = () => {
    setSelectedSemester("All Semesters");
    setSearchQuery("");
    toast.info("Filters reset to default");
  };

  const handleOpenAdd = () => {
    setEditingId(null);
    setFormData({
      subjectCode: "",
      subjectTitle: "",
      theoryHours: 2,
      practicalHours: 4,
      facultyLead: "Chef Rajesh Sharma",
      semester: "Semester III",
    });
    setIsModalOpen(true);
  };

  const handleEdit = (s: SubjectScheduleRecord) => {
    setEditingId(s.id);
    setFormData({
      subjectCode: s.subjectCode,
      subjectTitle: s.subjectTitle,
      theoryHours: s.theoryHours,
      practicalHours: s.practicalHours,
      facultyLead: s.facultyLead,
      semester: s.semester,
    });
    setIsModalOpen(true);
  };

  const handleDelete = (id: string) => {
    setSubjectSchedules((prev) => prev.filter((s) => s.id !== id));
    toast.success("Subject schedule allocation removed");
  };

  const handleSubmitForm = (e: React.FormEvent) => {
    e.preventDefault();
    if (!formData.subjectCode || !formData.subjectTitle) {
      toast.error("Please enter Subject Code and Title");
      return;
    }

    const totalHours = Number(formData.theoryHours) + Number(formData.practicalHours);

    if (editingId) {
      setSubjectSchedules((prev) =>
        prev.map((s) =>
          s.id === editingId
            ? {
                ...s,
                subjectCode: formData.subjectCode.toUpperCase(),
                subjectTitle: formData.subjectTitle,
                theoryHours: Number(formData.theoryHours),
                practicalHours: Number(formData.practicalHours),
                totalWeeklyHours: totalHours,
                facultyLead: formData.facultyLead,
                semester: formData.semester,
              }
            : s
        )
      );
      toast.success("Subject hours allocation updated");
    } else {
      const newRecord: SubjectScheduleRecord = {
        id: `sub-${Date.now()}`,
        subjectCode: formData.subjectCode.toUpperCase(),
        subjectTitle: formData.subjectTitle,
        theoryHours: Number(formData.theoryHours),
        practicalHours: Number(formData.practicalHours),
        totalWeeklyHours: totalHours,
        facultyLead: formData.facultyLead,
        semester: formData.semester,
      };
      setSubjectSchedules((prev) => [newRecord, ...prev]);
      toast.success("New subject contact hour quota allocated");
    }
    setIsModalOpen(false);
  };

  return (
    <div className="space-y-6 pb-12 font-sans bg-[#F8FAFC] min-h-screen">
      {/* 1. Header Bar */}
      <div>
        <div className="flex items-center gap-2 text-xs text-[#64748B] mb-2 font-medium">
          <Link href="/admin/dashboard" className="hover:text-[#1769E8] transition-colors">
            Home
          </Link>
          <span>&gt;</span>
          <Link href="/admin/timetable" className="hover:text-[#1769E8] transition-colors">
            Timetable Management
          </Link>
          <span>&gt;</span>
          <span className="text-[#071A4D] font-semibold">Subject-wise Hours</span>
        </div>

        <div className="flex flex-col md:flex-row md:items-center justify-between gap-4">
          <div>
            <h1 className="text-3xl font-bold font-heading text-[#071A4D] tracking-tight">
              Subject-wise Timetable Allocation
            </h1>
            <p className="text-[15px] text-[#64748B] mt-1 font-normal">
              Theory contact hours and practical kitchen lab quotas allocated per syllabus curriculum subject.
            </p>
          </div>

          <div className="flex items-center gap-3">
            <button
              onClick={() => toast.success("Exporting subject syllabus schedule to Excel...")}
              className="px-4 py-2 bg-white border border-[#E2E8F0] rounded-lg text-sm font-medium text-[#334155] hover:bg-slate-50 transition-colors flex items-center gap-2 shadow-xs cursor-pointer"
            >
              <FileSpreadsheet className="w-4 h-4 text-emerald-600" />
              <span>Export</span>
            </button>
            <button
              onClick={() => toast.info("Generating academic subject credits report...")}
              className="px-4 py-2 bg-white border border-[#E2E8F0] rounded-lg text-sm font-medium text-[#334155] hover:bg-slate-50 transition-colors flex items-center gap-2 shadow-xs cursor-pointer"
            >
              <FileText className="w-4 h-4 text-purple-600" />
              <span>Generate Report</span>
            </button>
            <button
              onClick={handleOpenAdd}
              className="px-5 py-2.5 bg-[#1769E8] hover:bg-[#1255be] text-white rounded-lg text-sm font-semibold transition-colors flex items-center gap-2 shadow-sm cursor-pointer"
            >
              <Plus className="w-4 h-4" />
              <span>+ Allocate Subject Hours</span>
            </button>
          </div>
        </div>
      </div>

      {/* 2. Filter Bar */}
      <div className="bg-white rounded-xl border border-[#E5EAF0] shadow-xs p-4">
        <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-12 gap-3.5 items-end">
          <div className="lg:col-span-4">
            <label className="block text-[11px] font-semibold text-[#64748B] uppercase tracking-wider mb-1.5">
              Semester
            </label>
            <select
              value={selectedSemester}
              onChange={(e) => setSelectedSemester(e.target.value)}
              className="w-full bg-white border border-[#E2E8F0] text-sm text-[#0F172A] rounded-lg px-3 py-2 font-medium focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all cursor-pointer"
            >
              <option>All Semesters</option>
              <option>Semester I</option>
              <option>Semester II</option>
              <option>Semester III</option>
              <option>Semester IV</option>
              <option>Semester V</option>
              <option>Semester VI</option>
            </select>
          </div>

          <div className="lg:col-span-6">
            <label className="block text-[11px] font-semibold text-[#64748B] uppercase tracking-wider mb-1.5">
              Search
            </label>
            <div className="relative">
              <Search className="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-[#94A3B8]" />
              <input
                type="text"
                value={searchQuery}
                onChange={(e) => setSearchQuery(e.target.value)}
                placeholder="Search subject code, title, faculty lead..."
                className="w-full pl-9 pr-3 py-2 bg-white border border-[#E2E8F0] text-sm text-[#0F172A] placeholder:text-[#94A3B8] rounded-lg focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all"
              />
            </div>
          </div>

          <div className="lg:col-span-2 flex items-center gap-2">
            <button
              onClick={() => toast.success(`Found ${filteredRecords.length} subject quotas`)}
              className="flex-1 bg-[#1769E8] hover:bg-[#1255be] text-white py-2 rounded-lg text-sm font-semibold transition-colors flex items-center justify-center gap-1.5 shadow-xs cursor-pointer"
            >
              <Search className="w-4 h-4" />
              <span>Search</span>
            </button>
            <button
              onClick={handleResetFilters}
              className="border border-[#E2E8F0] hover:bg-slate-50 text-[#64748B] p-2 rounded-lg text-sm font-medium transition-colors flex items-center justify-center cursor-pointer"
              title="Reset Filters"
            >
              <RotateCcw className="w-4 h-4" />
            </button>
          </div>
        </div>
      </div>

      {/* 3. Full-Width Table Card */}
      <div className="bg-white rounded-xl 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.5">
            <BookOpen className="w-5 h-5 text-[#1769E8]" />
            <h2 className="font-heading font-bold text-lg text-[#071A4D]">
              Subject Hours Allocation Roster ({filteredRecords.length})
            </h2>
          </div>
          <div className="flex items-center gap-2">
            <select
              value={pageSize}
              onChange={(e) => setPageSize(e.target.value)}
              className="text-xs bg-white border border-[#E2E8F0] rounded-lg px-2.5 py-1.5 font-medium text-[#475569] outline-none cursor-pointer"
            >
              <option value="10">10 per page</option>
              <option value="25">25 per page</option>
            </select>
          </div>
        </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] font-semibold">
                <th className="py-3 px-3.5 w-10 text-center">
                  <input
                    type="checkbox"
                    checked={
                      selectedRows.length === filteredRecords.length &&
                      filteredRecords.length > 0
                    }
                    onChange={handleSelectAll}
                    className="rounded border-[#CBD5E1] text-[#1769E8] focus:ring-0 cursor-pointer"
                  />
                </th>
                <th className="py-3 px-2 w-8 text-center text-[#64748B]">#</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Subject Code</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Subject Title</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D] text-center">Theory Hours</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D] text-center">Practical Hours</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D] text-center">Total Weekly Hrs</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Faculty In-Charge</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Semester</th>
                <th className="py-3 px-4 font-bold text-[#071A4D] text-right">Actions</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-[#E5EAF0]">
              {filteredRecords.length === 0 ? (
                <tr>
                  <td colSpan={10} className="py-12 text-center text-slate-400">
                    No subject allocations match your search criteria.
                  </td>
                </tr>
              ) : (
                filteredRecords.map((s, idx) => (
                  <tr
                    key={s.id}
                    className={`hover:bg-[#F8FAFC] transition-colors ${
                      selectedRows.includes(s.id) ? "bg-[#EFF6FF]/40" : ""
                    }`}
                  >
                    <td className="py-3.5 px-3.5 text-center">
                      <input
                        type="checkbox"
                        checked={selectedRows.includes(s.id)}
                        onChange={() => handleRowSelect(s.id)}
                        className="rounded border-[#CBD5E1] text-[#1769E8] focus:ring-0 cursor-pointer"
                      />
                    </td>
                    <td className="py-3.5 px-2 text-center text-[#94A3B8] font-medium">
                      {idx + 1}
                    </td>
                    <td className="py-3.5 px-3.5">
                      <span className="font-mono font-bold text-xs bg-[#F1F6FC] text-[#1769E8] px-2 py-0.5 rounded border border-[#D0E2FF]">
                        {s.subjectCode}
                      </span>
                    </td>
                    <td className="py-3.5 px-3.5 font-bold text-[#071A4D] text-sm">
                      {s.subjectTitle}
                    </td>
                    <td className="py-3.5 px-3.5 text-center font-mono font-bold text-[#334155]">
                      {s.theoryHours} hrs
                    </td>
                    <td className="py-3.5 px-3.5 text-center font-mono font-bold text-[#334155]">
                      {s.practicalHours} hrs
                    </td>
                    <td className="py-3.5 px-3.5 text-center font-mono font-bold text-sm text-[#1769E8]">
                      {s.totalWeeklyHours} hrs
                    </td>
                    <td className="py-3.5 px-3.5 font-medium text-[#334155]">
                      {s.facultyLead}
                    </td>
                    <td className="py-3.5 px-3.5 text-[#64748B] font-semibold">
                      {s.semester}
                    </td>
                    <td className="py-3.5 px-4 text-right">
                      <div className="flex items-center justify-end gap-1.5">
                        <button
                          onClick={() => setViewingRecord(s)}
                          className="p-1.5 text-[#64748B] hover:text-[#1769E8] hover:bg-[#EFF6FF] rounded-md transition-colors cursor-pointer"
                          title="View Details"
                        >
                          <Eye className="w-3.5 h-3.5" />
                        </button>
                        <button
                          onClick={() => handleEdit(s)}
                          className="p-1.5 text-[#64748B] hover:text-[#1769E8] hover:bg-[#EFF6FF] rounded-md transition-colors cursor-pointer"
                          title="Edit Quota"
                        >
                          <Edit2 className="w-3.5 h-3.5" />
                        </button>
                        <button
                          onClick={() => handleDelete(s.id)}
                          className="p-1.5 text-[#64748B] hover:text-rose-600 hover:bg-rose-50 rounded-md transition-colors cursor-pointer"
                          title="Delete Allocation"
                        >
                          <Trash2 className="w-3.5 h-3.5" />
                        </button>
                      </div>
                    </td>
                  </tr>
                ))
              )}
            </tbody>
          </table>
        </div>

        <div className="p-4 border-t border-[#E5EAF0] flex flex-col sm:flex-row items-center justify-between gap-3 text-xs text-[#64748B]">
          <div>
            Showing 1 to {filteredRecords.length} of {subjectSchedules.length} subject quotas
          </div>
          <div className="flex items-center gap-1">
            <button className="p-1.5 border border-[#E2E8F0] rounded-md hover:bg-slate-50 transition-colors cursor-pointer">
              <ChevronLeft className="w-3.5 h-3.5" />
            </button>
            <button className="px-3 py-1 bg-[#1769E8] text-white font-bold rounded-md text-xs">
              1
            </button>
            <button className="p-1.5 border border-[#E2E8F0] rounded-md hover:bg-slate-50 transition-colors cursor-pointer">
              <ChevronRight className="w-3.5 h-3.5" />
            </button>
          </div>
        </div>
      </div>

      {/* 4. MODAL: + Allocate / Edit Subject Hours */}
      {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="bg-white rounded-2xl border border-[#E5EAF0] shadow-xl max-w-lg w-full overflow-hidden">
            <div className="p-4 border-b border-[#E5EAF0] flex items-center justify-between bg-[#F8FAFC]">
              <div className="flex items-center gap-2">
                <BookOpen className="w-5 h-5 text-[#1769E8]" />
                <h3 className="font-heading font-bold text-base text-[#071A4D]">
                  {editingId ? "Edit Subject Hours" : "Allocate Subject Hours"}
                </h3>
              </div>
              <button
                onClick={() => setIsModalOpen(false)}
                className="p-1 text-[#64748B] hover:text-[#071A4D] rounded-md transition-colors cursor-pointer"
              >
                <X className="w-5 h-5" />
              </button>
            </div>

            <form onSubmit={handleSubmitForm} className="p-5 space-y-4 text-xs">
              <div className="grid grid-cols-2 gap-3">
                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Subject Code <span className="text-rose-500">*</span>
                  </label>
                  <input
                    type="text"
                    required
                    value={formData.subjectCode}
                    onChange={(e) => setFormData({ ...formData, subjectCode: e.target.value })}
                    placeholder="e.g. BHM201"
                    className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] uppercase font-mono focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all"
                  />
                </div>

                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Semester
                  </label>
                  <select
                    value={formData.semester}
                    onChange={(e) => setFormData({ ...formData, semester: e.target.value })}
                    className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all bg-white cursor-pointer"
                  >
                    <option>Semester I</option>
                    <option>Semester II</option>
                    <option>Semester III</option>
                    <option>Semester IV</option>
                    <option>Semester V</option>
                    <option>Semester VI</option>
                  </select>
                </div>
              </div>

              <div>
                <label className="block font-semibold text-[#334155] mb-1">
                  Subject Title <span className="text-rose-500">*</span>
                </label>
                <input
                  type="text"
                  required
                  value={formData.subjectTitle}
                  onChange={(e) => setFormData({ ...formData, subjectTitle: e.target.value })}
                  placeholder="e.g. Food Production Operations Practical"
                  className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] placeholder:text-[#94A3B8] focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all"
                />
              </div>

              <div className="grid grid-cols-2 gap-3">
                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Theory Hours (Weekly)
                  </label>
                  <input
                    type="number"
                    min="0"
                    max="10"
                    value={formData.theoryHours}
                    onChange={(e) => setFormData({ ...formData, theoryHours: parseFloat(e.target.value) || 0 })}
                    className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] font-mono focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all"
                  />
                </div>

                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Practical Hours (Weekly)
                  </label>
                  <input
                    type="number"
                    min="0"
                    max="20"
                    value={formData.practicalHours}
                    onChange={(e) => setFormData({ ...formData, practicalHours: parseFloat(e.target.value) || 0 })}
                    className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] font-mono focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all"
                  />
                </div>
              </div>

              <div>
                <label className="block font-semibold text-[#334155] mb-1">
                  Faculty In-Charge
                </label>
                <select
                  value={formData.facultyLead}
                  onChange={(e) => setFormData({ ...formData, facultyLead: e.target.value })}
                  className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all bg-white cursor-pointer"
                >
                  <option>Chef Rajesh Sharma</option>
                  <option>Prof. Ananya Sen</option>
                  <option>Chef Vikram Singh</option>
                  <option>Prof. Priya Nair</option>
                  <option>Ms. Moumita Bose</option>
                  <option>Chef Amit Roy</option>
                </select>
              </div>

              <div className="flex items-center gap-2 pt-3 border-t border-[#E5EAF0]">
                <button
                  type="button"
                  onClick={() => setIsModalOpen(false)}
                  className="flex-1 py-2 px-3 border border-[#E2E8F0] text-[#64748B] hover:bg-slate-50 rounded-lg text-xs font-semibold transition-colors cursor-pointer"
                >
                  Cancel
                </button>
                <button
                  type="submit"
                  className="flex-1 py-2 px-3 bg-[#1769E8] hover:bg-[#1255be] text-white rounded-lg text-xs font-semibold transition-colors flex items-center justify-center gap-1.5 shadow-xs cursor-pointer"
                >
                  <CheckCircle2 className="w-3.5 h-3.5" />
                  <span>{editingId ? "Update Allocation" : "Save Allocation"}</span>
                </button>
              </div>
            </form>
          </div>
        </div>
      )}

      {/* 5. MODAL: View Subject Quota Details */}
      {viewingRecord && (
        <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="bg-white rounded-2xl border border-[#E5EAF0] shadow-xl max-w-md w-full overflow-hidden">
            <div className="p-4 border-b border-[#E5EAF0] flex items-center justify-between bg-[#F8FAFC]">
              <div className="flex items-center gap-2">
                <BookOpen className="w-5 h-5 text-[#1769E8]" />
                <h3 className="font-heading font-bold text-base text-[#071A4D]">
                  Subject Load Specification
                </h3>
              </div>
              <button
                onClick={() => setViewingRecord(null)}
                className="p-1 text-[#64748B] hover:text-[#071A4D] rounded-md transition-colors cursor-pointer"
              >
                <X className="w-5 h-5" />
              </button>
            </div>

            <div className="p-5 space-y-4 text-xs">
              <div className="flex items-center justify-between pb-3 border-b border-[#E5EAF0]">
                <div>
                  <span className="font-mono text-xs font-bold px-2 py-0.5 rounded bg-blue-50 text-[#1769E8] border border-blue-200">
                    {viewingRecord.subjectCode}
                  </span>
                  <h4 className="font-heading font-bold text-base text-[#071A4D] mt-1.5">
                    {viewingRecord.subjectTitle}
                  </h4>
                  <p className="text-[11px] text-[#64748B]">{viewingRecord.semester}</p>
                </div>
              </div>

              <div className="space-y-2.5">
                <div className="flex items-center justify-between p-2.5 bg-[#F8FAFC] rounded-lg border border-[#E5EAF0]">
                  <span className="font-medium text-[#64748B]">Theory Contact Hours</span>
                  <span className="font-mono font-bold text-[#071A4D]">{viewingRecord.theoryHours}h / week</span>
                </div>
                <div className="flex items-center justify-between p-2.5 bg-[#F8FAFC] rounded-lg border border-[#E5EAF0]">
                  <span className="font-medium text-[#64748B]">Practical Contact Hours</span>
                  <span className="font-mono font-bold text-[#071A4D]">{viewingRecord.practicalHours}h / week</span>
                </div>
                <div className="flex items-center justify-between p-2.5 bg-[#F8FAFC] rounded-lg border border-[#E5EAF0]">
                  <span className="font-medium text-[#64748B]">Total Weekly Hours</span>
                  <span className="font-mono font-bold text-[#1769E8] text-sm">{viewingRecord.totalWeeklyHours} Hours</span>
                </div>
                <div className="flex items-center justify-between p-2.5 bg-[#F8FAFC] rounded-lg border border-[#E5EAF0]">
                  <span className="font-medium text-[#64748B]">Faculty In-Charge</span>
                  <span className="font-bold text-[#071A4D]">{viewingRecord.facultyLead}</span>
                </div>
              </div>

              <div className="flex items-center justify-end gap-2 pt-3 border-t border-[#E5EAF0]">
                <button
                  type="button"
                  onClick={() => {
                    const toEdit = viewingRecord;
                    setViewingRecord(null);
                    handleEdit(toEdit);
                  }}
                  className="py-2 px-3 border border-[#E2E8F0] hover:bg-slate-50 text-[#334155] rounded-lg text-xs font-semibold transition-colors cursor-pointer"
                >
                  Edit Quota
                </button>
                <button
                  type="button"
                  onClick={() => setViewingRecord(null)}
                  className="py-2 px-4 bg-[#1769E8] hover:bg-[#1255be] text-white rounded-lg text-xs font-semibold transition-colors cursor-pointer"
                >
                  Close
                </button>
              </div>
            </div>
          </div>
        </div>
      )}
    </div>
  );
}
