"use client";

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

interface PracticalSessionRecord {
  id: string;
  day: string;
  timing: string;
  facility: string;
  subjectCode: string;
  subjectName: string;
  batchGroup: string;
  instructorName: string;
  status: "Scheduled" | "In-Progress" | "Completed";
}

const initialPracticals: PracticalSessionRecord[] = [
  {
    id: "prac-1",
    day: "Monday",
    timing: "10:15 AM - 01:15 PM",
    facility: "Advanced Training Kitchen (ATK)",
    subjectCode: "BHM201",
    subjectName: "Advanced Culinary Practical",
    batchGroup: "Batch A - Group 1",
    instructorName: "Chef Rajesh Sharma",
    status: "Scheduled",
  },
  {
    id: "prac-2",
    day: "Tuesday",
    timing: "10:15 AM - 01:15 PM",
    facility: "Quantity Food Kitchen (QFK)",
    subjectCode: "BHM203",
    subjectName: "Regional Indian Cuisine Production",
    batchGroup: "Batch B - Group 2",
    instructorName: "Chef Amit Roy",
    status: "Scheduled",
  },
  {
    id: "prac-3",
    day: "Wednesday",
    timing: "01:45 PM - 04:45 PM",
    facility: "Training Restaurant (TR-1)",
    subjectCode: "BHM202",
    subjectName: "French Classical Service & Flambé",
    batchGroup: "Batch A - Group 2",
    instructorName: "Prof. Ananya Sen",
    status: "Scheduled",
  },
  {
    id: "prac-4",
    day: "Thursday",
    timing: "09:00 AM - 12:00 PM",
    facility: "Bakery & Confectionery Lab",
    subjectCode: "BHM113",
    subjectName: "Artisan Bread Making & Patisserie",
    batchGroup: "Batch C - Group 1",
    instructorName: "Chef Vikram Singh",
    status: "Scheduled",
  },
  {
    id: "prac-5",
    day: "Friday",
    timing: "10:15 AM - 01:15 PM",
    facility: "Basic Training Kitchen (BTK)",
    subjectCode: "BHM111",
    subjectName: "Classical Knife Cuts, Stocks & Sauces",
    batchGroup: "Batch A - Group 3",
    instructorName: "Chef Rajesh Sharma",
    status: "Scheduled",
  },
];

export default function PracticalSchedulePage() {
  const [practicals, setPracticals] = useState<PracticalSessionRecord[]>(initialPracticals);
  const [selectedDay, setSelectedDay] = useState("All Days");
  const [selectedFacility, setSelectedFacility] = useState("All Facilities");
  const [searchQuery, setSearchQuery] = useState("");
  const [selectedRows, setSelectedRows] = useState<string[]>([]);
  const [pageSize, setPageSize] = useState("10");

  // Modal State
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [viewingPrac, setViewingPrac] = useState<PracticalSessionRecord | null>(null);
  const [editingId, setEditingId] = useState<string | null>(null);

  const [formData, setFormData] = useState({
    day: "Monday",
    timing: "10:15 AM - 01:15 PM",
    facility: "Advanced Training Kitchen (ATK)",
    subjectCode: "BHM201",
    subjectName: "Advanced Culinary Practical",
    batchGroup: "Batch A - Group 1",
    instructorName: "Chef Rajesh Sharma",
    status: "Scheduled" as "Scheduled" | "In-Progress" | "Completed",
  });

  const filteredPracticals = practicals.filter((p) => {
    const matchesDay = selectedDay === "All Days" || p.day === selectedDay;
    const matchesFacility =
      selectedFacility === "All Facilities" || p.facility.includes(selectedFacility);
    const matchesSearch =
      searchQuery === "" ||
      p.subjectCode.toLowerCase().includes(searchQuery.toLowerCase()) ||
      p.subjectName.toLowerCase().includes(searchQuery.toLowerCase()) ||
      p.facility.toLowerCase().includes(searchQuery.toLowerCase()) ||
      p.batchGroup.toLowerCase().includes(searchQuery.toLowerCase()) ||
      p.instructorName.toLowerCase().includes(searchQuery.toLowerCase());
    return matchesDay && matchesFacility && matchesSearch;
  });

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

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

  const handleResetFilters = () => {
    setSelectedDay("All Days");
    setSelectedFacility("All Facilities");
    setSearchQuery("");
    toast.info("Filters reset to default");
  };

  const handleOpenAdd = () => {
    setEditingId(null);
    setFormData({
      day: "Monday",
      timing: "10:15 AM - 01:15 PM",
      facility: "Advanced Training Kitchen (ATK)",
      subjectCode: "BHM201",
      subjectName: "Advanced Culinary Practical",
      batchGroup: "Batch A - Group 1",
      instructorName: "Chef Rajesh Sharma",
      status: "Scheduled",
    });
    setIsModalOpen(true);
  };

  const handleEdit = (p: PracticalSessionRecord) => {
    setEditingId(p.id);
    setFormData({
      day: p.day,
      timing: p.timing,
      facility: p.facility,
      subjectCode: p.subjectCode,
      subjectName: p.subjectName,
      batchGroup: p.batchGroup,
      instructorName: p.instructorName,
      status: p.status,
    });
    setIsModalOpen(true);
  };

  const handleDelete = (id: string) => {
    setPracticals((prev) => prev.filter((p) => p.id !== id));
    toast.success("Practical session removed");
  };

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

    if (editingId) {
      setPracticals((prev) =>
        prev.map((p) =>
          p.id === editingId
            ? {
                ...p,
                day: formData.day,
                timing: formData.timing,
                facility: formData.facility,
                subjectCode: formData.subjectCode.toUpperCase(),
                subjectName: formData.subjectName,
                batchGroup: formData.batchGroup,
                instructorName: formData.instructorName,
                status: formData.status,
              }
            : p
        )
      );
      toast.success("Practical session updated");
    } else {
      const newPrac: PracticalSessionRecord = {
        id: `prac-${Date.now()}`,
        day: formData.day,
        timing: formData.timing,
        facility: formData.facility,
        subjectCode: formData.subjectCode.toUpperCase(),
        subjectName: formData.subjectName,
        batchGroup: formData.batchGroup,
        instructorName: formData.instructorName,
        status: formData.status,
      };
      setPracticals((prev) => [newPrac, ...prev]);
      toast.success("New practical session scheduled");
    }
    setIsModalOpen(false);
  };

  const getStatusBadge = (status: "Scheduled" | "In-Progress" | "Completed") => {
    switch (status) {
      case "Scheduled":
        return "bg-[#E0F2FE] text-[#0369A1] border-[#BAE6FD]";
      case "In-Progress":
        return "bg-[#FEF3C7] text-[#B45309] border-[#FDE68A]";
      case "Completed":
        return "bg-[#DCFCE7] text-[#15803D] border-[#BBF7D0]";
    }
  };

  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">Practical Schedule</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">
              Hospitality Practical Schedule
            </h1>
            <p className="text-[15px] text-[#64748B] mt-1 font-normal">
              Culinary kitchen, restaurant service, and bakery practical blocks, instructor rosters, and batch groups.
            </p>
          </div>

          <div className="flex items-center gap-3">
            <button
              onClick={() => toast.success("Exporting practicals 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 practical syllabus 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 Practical Session</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">
              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-4">
            <label className="block text-[11px] font-semibold text-[#64748B] uppercase tracking-wider mb-1.5">
              Practical Facility
            </label>
            <select
              value={selectedFacility}
              onChange={(e) => setSelectedFacility(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 Facilities</option>
              <option>Advanced Training Kitchen</option>
              <option>Quantity Food Kitchen</option>
              <option>Training Restaurant</option>
              <option>Bakery & Confectionery</option>
              <option>Basic Training Kitchen</option>
            </select>
          </div>

          <div className="lg:col-span-3">
            <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, batch, chef..."
                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 ${filteredPracticals.length} practical sessions`)}
              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">
            <CookingPot className="w-5 h-5 text-[#1769E8]" />
            <h2 className="font-heading font-bold text-lg text-[#071A4D]">
              Practical Rotations ({filteredPracticals.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 === filteredPracticals.length &&
                      filteredPracticals.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]">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]">Laboratory Facility</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Batch Group</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Chef Instructor</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D] text-center">Status</th>
                <th className="py-3 px-4 font-bold text-[#071A4D] text-right">Actions</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-[#E5EAF0]">
              {filteredPracticals.length === 0 ? (
                <tr>
                  <td colSpan={9} className="py-12 text-center text-slate-400">
                    No practical rotations match your search criteria.
                  </td>
                </tr>
              ) : (
                filteredPracticals.map((p, idx) => (
                  <tr
                    key={p.id}
                    className={`hover:bg-[#F8FAFC] transition-colors ${
                      selectedRows.includes(p.id) ? "bg-[#EFF6FF]/40" : ""
                    }`}
                  >
                    <td className="py-3.5 px-3.5 text-center">
                      <input
                        type="checkbox"
                        checked={selectedRows.includes(p.id)}
                        onChange={() => handleRowSelect(p.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">
                      <div className="font-bold text-[#071A4D] text-xs">{p.day}</div>
                      <div className="font-mono text-[11px] text-[#64748B]">{p.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]">
                          {p.subjectCode}
                        </span>
                        <span className="font-bold text-[#071A4D] text-xs">
                          {p.subjectName}
                        </span>
                      </div>
                    </td>
                    <td className="py-3.5 px-3.5">
                      <div className="font-semibold text-[#071A4D] flex items-center gap-1.5">
                        <MapPin className="w-3.5 h-3.5 text-[#1769E8]" />
                        <span>{p.facility}</span>
                      </div>
                    </td>
                    <td className="py-3.5 px-3.5">
                      <span className="font-semibold text-[#334155]">{p.batchGroup}</span>
                    </td>
                    <td className="py-3.5 px-3.5 text-[#334155] font-semibold">
                      {p.instructorName}
                    </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-[11px] font-bold border ${getStatusBadge(
                          p.status
                        )}`}
                      >
                        {p.status}
                      </span>
                    </td>
                    <td className="py-3.5 px-4 text-right">
                      <div className="flex items-center justify-end gap-1.5">
                        <button
                          onClick={() => setViewingPrac(p)}
                          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(p)}
                          className="p-1.5 text-[#64748B] hover:text-[#1769E8] hover:bg-[#EFF6FF] rounded-md transition-colors cursor-pointer"
                          title="Edit Session"
                        >
                          <Edit2 className="w-3.5 h-3.5" />
                        </button>
                        <button
                          onClick={() => handleDelete(p.id)}
                          className="p-1.5 text-[#64748B] hover:text-rose-600 hover:bg-rose-50 rounded-md transition-colors cursor-pointer"
                          title="Delete Session"
                        >
                          <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 {filteredPracticals.length} of {practicals.length} practical rotations
          </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 Practical Session */}
      {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">
                <CookingPot className="w-5 h-5 text-[#1769E8]" />
                <h3 className="font-heading font-bold text-base text-[#071A4D]">
                  {editingId ? "Edit Practical Session" : "Schedule Practical Session"}
                </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">
                    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>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Shift Timings
                  </label>
                  <input
                    type="text"
                    value={formData.timing}
                    onChange={(e) => setFormData({ ...formData, timing: e.target.value })}
                    placeholder="e.g. 10:15 AM - 01:15 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>

              <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">
                    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. Advanced Culinary 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>

              <div>
                <label className="block font-semibold text-[#334155] mb-1">
                  Laboratory Facility
                </label>
                <select
                  value={formData.facility}
                  onChange={(e) => setFormData({ ...formData, facility: 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>Advanced Training Kitchen (ATK)</option>
                  <option>Quantity Food Kitchen (QFK)</option>
                  <option>Training Restaurant (TR-1)</option>
                  <option>Bakery & Confectionery Lab</option>
                  <option>Basic Training Kitchen (BTK)</option>
                </select>
              </div>

              <div className="grid grid-cols-2 gap-3">
                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Batch Group
                  </label>
                  <input
                    type="text"
                    value={formData.batchGroup}
                    onChange={(e) => setFormData({ ...formData, batchGroup: e.target.value })}
                    placeholder="e.g. Batch A - Group 1"
                    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">
                    Chef Instructor
                  </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>Chef Amit Roy</option>
                    <option>Prof. Ananya Sen</option>
                    <option>Chef Vikram Singh</option>
                  </select>
                </div>
              </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 Practical" : "Save Practical"}</span>
                </button>
              </div>
            </form>
          </div>
        </div>
      )}

      {/* 5. MODAL: View Practical Details */}
      {viewingPrac && (
        <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">
                <CookingPot className="w-5 h-5 text-[#1769E8]" />
                <h3 className="font-heading font-bold text-base text-[#071A4D]">
                  Practical Session Details
                </h3>
              </div>
              <button
                onClick={() => setViewingPrac(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">
                    {viewingPrac.subjectCode}
                  </span>
                  <h4 className="font-heading font-bold text-base text-[#071A4D] mt-1.5">
                    {viewingPrac.subjectName}
                  </h4>
                </div>
                <span
                  className={`px-2.5 py-1 rounded-full text-xs font-bold border ${getStatusBadge(
                    viewingPrac.status
                  )}`}
                >
                  {viewingPrac.status}
                </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]">{viewingPrac.day} • {viewingPrac.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]">Facility</span>
                  <span className="font-bold text-[#071A4D]">{viewingPrac.facility}</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]">Batch Group</span>
                  <span className="font-semibold text-[#1769E8]">{viewingPrac.batchGroup}</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]">Chef In-Charge</span>
                  <span className="font-bold text-[#071A4D]">{viewingPrac.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 = viewingPrac;
                    setViewingPrac(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 Session
                </button>
                <button
                  type="button"
                  onClick={() => setViewingPrac(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>
  );
}
