"use client";

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

interface SemesterScheduleItem {
  id: string;
  semester: string;
  day: string;
  timing: string;
  subjectCode: string;
  subjectName: string;
  hours: number;
  type: "THEORY" | "PRACTICAL";
  roomFacility: string;
  instructorName: string;
}

const initialSemesterSchedules: SemesterScheduleItem[] = [
  {
    id: "sem-1",
    semester: "Semester III",
    day: "Monday",
    timing: "09:00 AM - 01:00 PM",
    subjectCode: "BHM201",
    subjectName: "Food Production Operations Practical",
    hours: 4.0,
    type: "PRACTICAL",
    roomFacility: "Basic Training Kitchen B",
    instructorName: "Chef Rajesh Sharma",
  },
  {
    id: "sem-2",
    semester: "Semester III",
    day: "Monday",
    timing: "02:00 PM - 03:00 PM",
    subjectCode: "BHM202",
    subjectName: "Food & Beverage Service Theory",
    hours: 1.0,
    type: "THEORY",
    roomFacility: "Lecture Hall 101",
    instructorName: "Prof. Ananya Sen",
  },
  {
    id: "sem-3",
    semester: "Semester III",
    day: "Tuesday",
    timing: "09:00 AM - 01:00 PM",
    subjectCode: "BHM203",
    subjectName: "Bakery & Confectionery Practical",
    hours: 4.0,
    type: "PRACTICAL",
    roomFacility: "Bakery Lab 1",
    instructorName: "Chef Vikram Singh",
  },
  {
    id: "sem-4",
    semester: "Semester III",
    day: "Wednesday",
    timing: "11:00 AM - 12:00 PM",
    subjectCode: "BHM204",
    subjectName: "Front Office Operations Theory",
    hours: 1.0,
    type: "THEORY",
    roomFacility: "Smart Lecture Room 204",
    instructorName: "Prof. Priya Nair",
  },
  {
    id: "sem-5",
    semester: "Semester III",
    day: "Thursday",
    timing: "10:00 AM - 01:00 PM",
    subjectCode: "BHM205",
    subjectName: "Accommodation Operations Practical",
    hours: 3.0,
    type: "PRACTICAL",
    roomFacility: "Housekeeping Suite Model",
    instructorName: "Ms. Moumita Bose",
  },
  {
    id: "sem-6",
    semester: "Semester I",
    day: "Monday",
    timing: "09:00 AM - 10:00 AM",
    subjectCode: "BHM111",
    subjectName: "Foundation Food Production Theory",
    hours: 1.0,
    type: "THEORY",
    roomFacility: "Lecture Hall 102",
    instructorName: "Chef Amit Roy",
  },
];

export default function SemesterTimetablePage() {
  const [schedules, setSchedules] = useState<SemesterScheduleItem[]>(initialSemesterSchedules);
  const [selectedSemester, setSelectedSemester] = useState("All Semesters");
  const [selectedDay, setSelectedDay] = useState("All Days");
  const [selectedType, setSelectedType] = useState("All Types");
  const [searchQuery, setSearchQuery] = useState("");
  const [selectedRows, setSelectedRows] = useState<string[]>([]);
  const [pageSize, setPageSize] = useState("10");

  // Modal State
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [viewingItem, setViewingItem] = useState<SemesterScheduleItem | null>(null);
  const [editingId, setEditingId] = useState<string | null>(null);

  const [formData, setFormData] = useState({
    semester: "Semester III",
    day: "Monday",
    timing: "09:00 AM - 01:00 PM",
    subjectCode: "BHM201",
    subjectName: "Food Production Operations Practical",
    hours: 4.0,
    type: "PRACTICAL" as "THEORY" | "PRACTICAL",
    roomFacility: "Basic Training Kitchen B",
    instructorName: "Chef Rajesh Sharma",
  });

  const filteredSchedules = schedules.filter((s) => {
    const matchesSem = selectedSemester === "All Semesters" || s.semester === selectedSemester;
    const matchesDay = selectedDay === "All Days" || s.day === selectedDay;
    const matchesType = selectedType === "All Types" || s.type === selectedType;
    const matchesSearch =
      searchQuery === "" ||
      s.subjectCode.toLowerCase().includes(searchQuery.toLowerCase()) ||
      s.subjectName.toLowerCase().includes(searchQuery.toLowerCase()) ||
      s.roomFacility.toLowerCase().includes(searchQuery.toLowerCase()) ||
      s.instructorName.toLowerCase().includes(searchQuery.toLowerCase());
    return matchesSem && matchesDay && matchesType && matchesSearch;
  });

  const handleSelectAll = (e: React.ChangeEvent<HTMLInputElement>) => {
    if (e.target.checked) {
      setSelectedRows(filteredSchedules.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");
    setSelectedDay("All Days");
    setSelectedType("All Types");
    setSearchQuery("");
    toast.info("Filters reset to default");
  };

  const handleOpenAdd = () => {
    setEditingId(null);
    setFormData({
      semester: "Semester III",
      day: "Monday",
      timing: "09:00 AM - 01:00 PM",
      subjectCode: "BHM201",
      subjectName: "Food Production Operations Practical",
      hours: 4.0,
      type: "PRACTICAL",
      roomFacility: "Basic Training Kitchen B",
      instructorName: "Chef Rajesh Sharma",
    });
    setIsModalOpen(true);
  };

  const handleEdit = (s: SemesterScheduleItem) => {
    setEditingId(s.id);
    setFormData({
      semester: s.semester,
      day: s.day,
      timing: s.timing,
      subjectCode: s.subjectCode,
      subjectName: s.subjectName,
      hours: s.hours,
      type: s.type,
      roomFacility: s.roomFacility,
      instructorName: s.instructorName,
    });
    setIsModalOpen(true);
  };

  const handleDelete = (id: string) => {
    setSchedules((prev) => prev.filter((s) => s.id !== id));
    toast.success("Semester slot removed");
  };

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

    if (editingId) {
      setSchedules((prev) =>
        prev.map((s) =>
          s.id === editingId
            ? {
                ...s,
                semester: formData.semester,
                day: formData.day,
                timing: formData.timing,
                subjectCode: formData.subjectCode.toUpperCase(),
                subjectName: formData.subjectName,
                hours: Number(formData.hours),
                type: formData.type,
                roomFacility: formData.roomFacility,
                instructorName: formData.instructorName,
              }
            : s
        )
      );
      toast.success("Semester slot updated successfully");
    } else {
      const newItem: SemesterScheduleItem = {
        id: `sem-${Date.now()}`,
        semester: formData.semester,
        day: formData.day,
        timing: formData.timing,
        subjectCode: formData.subjectCode.toUpperCase(),
        subjectName: formData.subjectName,
        hours: Number(formData.hours),
        type: formData.type,
        roomFacility: formData.roomFacility,
        instructorName: formData.instructorName,
      };
      setSchedules((prev) => [newItem, ...prev]);
      toast.success("New semester schedule slot created");
    }
    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">Semester Timetable</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">
              Semester Master Academic Schedule
            </h1>
            <p className="text-[15px] text-[#64748B] mt-1 font-normal">
              Curriculum-wide contact hour distribution across theoretical lectures and practical kitchen labs by semester.
            </p>
          </div>

          <div className="flex items-center gap-3">
            <button
              onClick={() => toast.success("Exporting semester 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 semester schedule 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>+ Add Semester Slot</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-3">
            <label className="block text-[11px] font-semibold text-[#64748B] uppercase tracking-wider mb-1.5">
              Semester Term
            </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-3">
            <label className="block text-[11px] font-semibold text-[#64748B] uppercase tracking-wider mb-1.5">
              Day of Week
            </label>
            <select
              value={selectedDay}
              onChange={(e) => setSelectedDay(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 Days</option>
              <option>Monday</option>
              <option>Tuesday</option>
              <option>Wednesday</option>
              <option>Thursday</option>
              <option>Friday</option>
            </select>
          </div>

          <div className="lg:col-span-2">
            <label className="block text-[11px] font-semibold text-[#64748B] uppercase tracking-wider mb-1.5">
              Type
            </label>
            <select
              value={selectedType}
              onChange={(e) => setSelectedType(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 Types</option>
              <option value="THEORY">Theory</option>
              <option value="PRACTICAL">Practical</option>
            </select>
          </div>

          <div className="lg:col-span-2">
            <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 slot..."
                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 ${filteredSchedules.length} semester slots`)}
              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">
            <GraduationCap className="w-5 h-5 text-[#1769E8]" />
            <h2 className="font-heading font-bold text-lg text-[#071A4D]">
              Semester Academic Schedule Slots ({filteredSchedules.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 === filteredSchedules.length &&
                      filteredSchedules.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]">Semester Term</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Day &amp; Timings</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Subject &amp; Code</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Room / Facility</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Instructor In-Charge</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D] text-center">Hours</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D] text-center">Type</th>
                <th className="py-3 px-4 font-bold text-[#071A4D] text-right">Actions</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-[#E5EAF0]">
              {filteredSchedules.length === 0 ? (
                <tr>
                  <td colSpan={10} className="py-12 text-center text-slate-400">
                    No semester schedule slots match your search criteria.
                  </td>
                </tr>
              ) : (
                filteredSchedules.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 font-bold text-[#071A4D]">
                      {s.semester}
                    </td>
                    <td className="py-3.5 px-3.5">
                      <div className="font-bold text-[#334155]">{s.day}</div>
                      <div className="font-mono text-[11px] text-[#64748B]">{s.timing}</div>
                    </td>
                    <td className="py-3.5 px-3.5">
                      <div className="flex items-center gap-1.5">
                        <span className="font-mono font-bold text-[10px] bg-[#F1F6FC] text-[#1769E8] px-1.5 py-0.5 rounded border border-[#D0E2FF]">
                          {s.subjectCode}
                        </span>
                        <span className="font-bold text-[#071A4D] text-xs">
                          {s.subjectName}
                        </span>
                      </div>
                    </td>
                    <td className="py-3.5 px-3.5 text-[#475569]">
                      <div className="flex items-center gap-1">
                        <MapPin className="w-3.5 h-3.5 text-[#94A3B8]" />
                        <span>{s.roomFacility}</span>
                      </div>
                    </td>
                    <td className="py-3.5 px-3.5 text-[#334155] font-semibold">
                      {s.instructorName}
                    </td>
                    <td className="py-3.5 px-3.5 text-center font-mono font-bold text-[#071A4D]">
                      {s.hours}h
                    </td>
                    <td className="py-3.5 px-3.5 text-center">
                      <span
                        className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-[10px] font-bold border ${
                          s.type === "PRACTICAL"
                            ? "bg-[#FEF3C7] text-[#B45309] border-[#FDE68A]"
                            : "bg-[#E0F2FE] text-[#0369A1] border-[#BAE6FD]"
                        }`}
                      >
                        {s.type}
                      </span>
                    </td>
                    <td className="py-3.5 px-4 text-right">
                      <div className="flex items-center justify-end gap-1.5">
                        <button
                          onClick={() => setViewingItem(s)}
                          className="p-1.5 text-[#64748B] hover:text-[#1769E8] hover:bg-[#EFF6FF] rounded-md transition-colors cursor-pointer"
                          title="View Slot"
                        >
                          <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 Slot"
                        >
                          <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 Slot"
                        >
                          <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 {filteredSchedules.length} of {schedules.length} slots
          </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: + Add / Edit Semester Slot */}
      {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">
                <GraduationCap className="w-5 h-5 text-[#1769E8]" />
                <h3 className="font-heading font-bold text-base text-[#071A4D]">
                  {editingId ? "Edit Semester Slot" : "Add Semester Timetable Slot"}
                </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">
                    Semester Term <span className="text-rose-500">*</span>
                  </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>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Day of Week <span className="text-rose-500">*</span>
                  </label>
                  <select
                    value={formData.day}
                    onChange={(e) => setFormData({ ...formData, day: 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>Monday</option>
                    <option>Tuesday</option>
                    <option>Wednesday</option>
                    <option>Thursday</option>
                    <option>Friday</option>
                  </select>
                </div>
              </div>

              <div className="grid grid-cols-2 gap-3">
                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Scheduled Timing
                  </label>
                  <input
                    type="text"
                    value={formData.timing}
                    onChange={(e) => setFormData({ ...formData, timing: e.target.value })}
                    placeholder="e.g. 09:00 AM - 01:00 PM"
                    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 font-mono"
                  />
                </div>

                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Instruction Type
                  </label>
                  <select
                    value={formData.type}
                    onChange={(e) =>
                      setFormData({
                        ...formData,
                        type: e.target.value as "THEORY" | "PRACTICAL",
                      })
                    }
                    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 value="PRACTICAL">Practical</option>
                    <option value="THEORY">Theory</option>
                  </select>
                </div>
              </div>

              <div className="grid grid-cols-1 sm:grid-cols-3 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 className="sm:col-span-2">
                  <label className="block font-semibold text-[#334155] mb-1">
                    Subject Name <span className="text-rose-500">*</span>
                  </label>
                  <input
                    type="text"
                    required
                    value={formData.subjectName}
                    onChange={(e) => setFormData({ ...formData, subjectName: e.target.value })}
                    placeholder="e.g. Food Production Operations"
                    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>

              <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Room / Facility
                  </label>
                  <input
                    type="text"
                    value={formData.roomFacility}
                    onChange={(e) => setFormData({ ...formData, roomFacility: e.target.value })}
                    placeholder="e.g. Basic Training Kitchen B"
                    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"
                  />
                </div>

                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Duration Hours
                  </label>
                  <input
                    type="number"
                    step="0.5"
                    min="0.5"
                    max="8"
                    value={formData.hours}
                    onChange={(e) => setFormData({ ...formData, hours: parseFloat(e.target.value) || 1 })}
                    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">
                  Instructor In-Charge
                </label>
                <select
                  value={formData.instructorName}
                  onChange={(e) => setFormData({ ...formData, instructorName: 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 Slot" : "Save Slot"}</span>
                </button>
              </div>
            </form>
          </div>
        </div>
      )}

      {/* 5. MODAL: View Semester Slot */}
      {viewingItem && (
        <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">
                <GraduationCap className="w-5 h-5 text-[#1769E8]" />
                <h3 className="font-heading font-bold text-base text-[#071A4D]">
                  Semester Schedule Overview
                </h3>
              </div>
              <button
                onClick={() => setViewingItem(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">
                    {viewingItem.subjectCode}
                  </span>
                  <h4 className="font-heading font-bold text-base text-[#071A4D] mt-1.5">
                    {viewingItem.subjectName}
                  </h4>
                  <p className="text-[11px] text-[#64748B]">{viewingItem.semester}</p>
                </div>
                <span
                  className={`px-2.5 py-1 rounded-full text-xs font-bold border ${
                    viewingItem.type === "PRACTICAL"
                      ? "bg-[#FEF3C7] text-[#B45309] border-[#FDE68A]"
                      : "bg-[#E0F2FE] text-[#0369A1] border-[#BAE6FD]"
                  }`}
                >
                  {viewingItem.type}
                </span>
              </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]">Day &amp; Timings</span>
                  <span className="font-bold text-[#071A4D]">{viewingItem.day} • {viewingItem.timing}</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]">Room / Venue</span>
                  <span className="font-bold text-[#071A4D]">{viewingItem.roomFacility}</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]">Weekly Contact Load</span>
                  <span className="font-mono font-bold text-[#1769E8]">{viewingItem.hours} Hours / 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]">Instructor In-Charge</span>
                  <span className="font-bold text-[#071A4D]">{viewingItem.instructorName}</span>
                </div>
              </div>

              <div className="flex items-center justify-end gap-2 pt-3 border-t border-[#E5EAF0]">
                <button
                  type="button"
                  onClick={() => {
                    const toEdit = viewingItem;
                    setViewingItem(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 Slot
                </button>
                <button
                  type="button"
                  onClick={() => setViewingItem(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>
  );
}
