"use client";

import React, { useState } from "react";
import Link from "next/link";
import {
  Layers,
  Search,
  RotateCcw,
  Plus,
  Eye,
  ChevronRight,
  FileSpreadsheet,
  Building,
} from "lucide-react";
import { toast } from "sonner";

interface CampusFacility {
  id: string;
  name: string;
  category: "Kitchen Lab" | "Restaurant / Bar Lab" | "Classroom" | "Computer Lab";
  location: string;
  capacity: number;
  status: "Operational" | "Maintenance";
}

const sampleFacilities: CampusFacility[] = [
  {
    id: "fac-1",
    name: "Advanced Training Kitchen (ATK)",
    category: "Kitchen Lab",
    location: "Block A - Ground Floor",
    capacity: 35,
    status: "Operational",
  },
  {
    id: "fac-2",
    name: "Quantity Food Kitchen (QFK)",
    category: "Kitchen Lab",
    location: "Block A - 1st Floor",
    capacity: 40,
    status: "Operational",
  },
  {
    id: "fac-3",
    name: "Basic Training Kitchen (BTK)",
    category: "Kitchen Lab",
    location: "Block B - Ground Floor",
    capacity: 30,
    status: "Operational",
  },
  {
    id: "fac-4",
    name: "Training Restaurant & Bar Lab",
    category: "Restaurant / Bar Lab",
    location: "Block A - 2nd Floor",
    capacity: 50,
    status: "Operational",
  },
  {
    id: "fac-5",
    name: "Smart Lecture Hall 101",
    category: "Classroom",
    location: "Academic Block - 1st Floor",
    capacity: 65,
    status: "Operational",
  },
];

export default function FacilitiesPage() {
  const [search, setSearch] = useState("");

  const filtered = sampleFacilities.filter(
    (f) =>
      f.name.toLowerCase().includes(search.toLowerCase()) ||
      f.category.toLowerCase().includes(search.toLowerCase())
  );

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

          <h1 className="text-3xl font-bold font-heading text-[#071A4D] tracking-tight">
            Campus Infrastructure & Practical Labs
          </h1>
          <p className="text-[15px] text-[#64748B] mt-1 font-normal font-sans">
            Manage practical training kitchens, bakery laboratories, demo restaurants, and classrooms.
          </p>
        </div>

        <div className="flex items-center gap-2.5 flex-wrap">
          <button
            onClick={() => toast.success("Exporting Facilities List...")}
            className="inline-flex items-center gap-1.5 px-3.5 py-2 text-xs font-semibold text-[#071A4D] bg-white border border-[#E5EAF0] rounded-lg hover:bg-slate-50 transition-colors shadow-2xs cursor-pointer"
          >
            <FileSpreadsheet className="w-4 h-4 text-emerald-600" />
            <span>Export List</span>
          </button>
          <button
            onClick={() => toast.info("Register facility modal...")}
            className="inline-flex items-center gap-1.5 px-4 py-2 text-xs font-semibold text-white bg-[#1769E8] hover:bg-[#1257C7] rounded-lg shadow-sm transition-colors cursor-pointer"
          >
            <Plus className="w-4 h-4" />
            <span>+ Add Facility Lab</span>
          </button>
        </div>
      </div>

      <div className="rounded-xl bg-white border border-[#E5EAF0] shadow-2xs overflow-hidden">
        <div className="p-4 border-b border-[#E5EAF0] flex items-center justify-between">
          <div className="flex items-center gap-2">
            <Layers className="w-4 h-4 text-[#1769E8]" />
            <h2 className="text-sm font-bold font-heading text-[#071A4D]">
              Facilities & Training Kitchens ({filtered.length})
            </h2>
          </div>
          <span className="text-xs text-[#64748B] font-medium">Campus Infrastructure</span>
        </div>

        <div className="overflow-x-auto">
          <table className="w-full text-left text-xs border-collapse">
            <thead>
              <tr className="bg-[#F8FAFC] border-b border-[#E5EAF0] text-[#64748B] uppercase tracking-wider font-bold text-[11px]">
                <th className="py-3 px-3">Facility Name</th>
                <th className="py-3 px-3">Category</th>
                <th className="py-3 px-3">Location</th>
                <th className="py-3 px-3 text-center">Batch Capacity</th>
                <th className="py-3 px-3 text-center">Status</th>
                <th className="py-3 px-3 text-center">Actions</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-[#E5EAF0] text-[#334155]">
              {filtered.map((item) => (
                <tr key={item.id} className="hover:bg-[#F8FAFC] transition-colors">
                  <td className="py-3.5 px-3 font-semibold text-[#071A4D]">
                    {item.name}
                  </td>
                  <td className="py-3.5 px-3 text-[#334155]">
                    {item.category}
                  </td>
                  <td className="py-3.5 px-3 text-[#64748B]">
                    {item.location}
                  </td>
                  <td className="py-3.5 px-3 text-center font-bold text-[#071A4D]">
                    {item.capacity} students
                  </td>
                  <td className="py-3.5 px-3 text-center">
                    <span className="inline-block px-2.5 py-0.5 rounded-full text-[11px] font-semibold bg-[#DCFCE7] text-[#15803D]">
                      {item.status}
                    </span>
                  </td>
                  <td className="py-3.5 px-3 text-center">
                    <button
                      onClick={() => toast.info(`Viewing facility: ${item.name}`)}
                      className="p-1 rounded text-[#1769E8] hover:bg-blue-50 transition-colors"
                      title="View Facility"
                    >
                      <Eye className="w-3.5 h-3.5" />
                    </button>
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      </div>
    </div>
  );
}
