"use client";

import React, { useState } from "react";
import Link from "next/link";
import {
  Search,
  RotateCcw,
  Plus,
  Eye,
  Edit2,
  Trash2,
  ChevronLeft,
  ChevronRight,
  FileSpreadsheet,
  FileText,
  HeartPulse,
  FileCheck2,
  CheckCircle2,
  Sliders,
  Award,
  RefreshCw,
  History,
  ShieldCheck,
} from "lucide-react";
import { toast } from "sonner";

export default function AdjustmentHistoryPage() {
  const [searchQuery, setSearchQuery] = useState("");
  const [selectedRows, setSelectedRows] = useState<number[]>([]);
  const [pageSize, setPageSize] = useState("10");

  const allRows = [
    ["2025-2026","Even Semester (II, IV, VI)","84 Cases","62 Cases","14 Cases","8 Cases","90.47%"],
    ["2025-2026","Odd Semester (I, III, V)","92 Cases","70 Cases","12 Cases","10 Cases","89.13%"],
    ["2024-2025","Even Semester (II, IV, VI)","78 Cases","58 Cases","11 Cases","9 Cases","88.46%"],
    ["2024-2025","Odd Semester (I, III, V)","88 Cases","66 Cases","13 Cases","9 Cases","89.77%"],
    ["2023-2024","Annual Academic Cycle","164 Cases","126 Cases","22 Cases","16 Cases","90.24%"]
  ];

  const filteredRows = allRows.filter((r) =>
    searchQuery === "" || r.some((c) => String(c).toLowerCase().includes(searchQuery.toLowerCase()))
  );

  const handleSelectAll = (e: React.ChangeEvent<HTMLInputElement>) => {
    if (e.target.checked) {
      setSelectedRows(filteredRows.map((_, i) => i));
    } else {
      setSelectedRows([]);
    }
  };

  const handleRowSelect = (idx: number) => {
    setSelectedRows((prev) =>
      prev.includes(idx) ? prev.filter((i) => i !== idx) : [...prev, idx]
    );
  };

  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/adjustments/medical" className="hover:text-[#1769E8] transition-colors">
            Medical & Condonation
          </Link>
          <span>&gt;</span>
          <span className="text-[#071A4D] font-semibold">Adjustment History</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">
              Adjustment History
            </h1>
            <p className="text-[15px] text-[#64748B] mt-1 font-normal">
              Historical archive of student attendance adjustments, petitions, and audit findings.
            </p>
          </div>

          <div className="flex items-center gap-3">
            <button
              onClick={() => toast.success("Exporting data 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 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={() => toast.info("Opening Filter History dialog...")}
              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>+ Filter History</span>
            </button>
          </div>
        </div>
      </div>

      {/* Subcategory Navigation Tabs */}
      <div className="flex items-center gap-1.5 overflow-x-auto pb-1 border-b border-[#E2E8F0]">
        {[
          { label: "Requests", href: "/admin/adjustments/medical", icon: HeartPulse, active: false },
          { label: "Certificate Reference", href: "/admin/adjustments/medical/certificates", icon: FileCheck2, active: false },
          { label: "Approvals", href: "/admin/adjustments/medical/approvals", icon: CheckCircle2, active: false },
          { label: "Configurable %", href: "/admin/adjustments/medical/policy", icon: Sliders, active: false },
          { label: "Condonations", href: "/admin/adjustments/condonation", icon: FileText, active: false },
          { label: "Approved Hours", href: "/admin/adjustments/approved", icon: Award, active: false },
          { label: "Recalculation", href: "/admin/adjustments/recalculation", icon: RefreshCw, active: false },
          { label: "History", href: "/admin/adjustments/history", icon: History, active: true },
          { label: "Audit Trail", href: "/admin/adjustments/audit-trail", icon: ShieldCheck, active: false },
        ].map((tab) => {
          const Icon = tab.icon;
          return (
            <Link
              key={tab.href}
              href={tab.href}
              className={`flex items-center gap-1.5 px-3 py-2 rounded-lg text-xs font-semibold whitespace-nowrap transition-all ${
                tab.active
                  ? "bg-[#081A2E] text-white shadow-xs"
                  : "bg-white text-[#64748B] hover:text-[#071A4D] hover:bg-slate-100 border border-[#E2E8F0]"
              }`}
            >
              <Icon className="w-3.5 h-3.5" />
              <span>{tab.label}</span>
            </Link>
          );
        })}
      </div>

      {/* 2. Filter Bar */}
      <div className="bg-white rounded-xl border border-[#E5EAF0] shadow-xs p-4">
        <div className="grid grid-cols-1 md:grid-cols-12 gap-3.5 items-end">
          <div className="md:col-span-10">
            <label className="block text-[11px] font-semibold text-[#64748B] uppercase tracking-wider mb-1.5">
              Search Records
            </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 records, codes, titles or names..."
                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="md:col-span-2 flex items-center gap-2">
            <button
              onClick={() => toast.success(`Found ${filteredRows.length} matching records`)}
              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={() => setSearchQuery("")}
              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. Data 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">
            <h2 className="font-heading font-bold text-lg text-[#071A4D]">
              Adjustment History ({filteredRows.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 === filteredRows.length && filteredRows.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="px-4 py-3 text-left font-semibold text-[#071A4D]">Academic Year</th>
                  <th className="px-4 py-3 text-left font-semibold text-[#071A4D]">Semester</th>
                  <th className="px-4 py-3 text-left font-semibold text-[#071A4D]">Total Petitions</th>
                  <th className="px-4 py-3 text-left font-semibold text-[#071A4D]">Medical Approved</th>
                  <th className="px-4 py-3 text-left font-semibold text-[#071A4D]">Condoned</th>
                  <th className="px-4 py-3 text-left font-semibold text-[#071A4D]">Rejected</th>
                  <th className="px-4 py-3 text-left font-semibold text-[#071A4D]">Success Rate</th>
                <th className="py-3 px-4 font-bold text-[#071A4D] text-right">Actions</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-[#E5EAF0]">
              <tr key={0} className="border-b border-[#E5EAF0] hover:bg-slate-50/80 transition-colors">
                    <td className="px-4 py-3 font-semibold text-[#071A4D] whitespace-nowrap">2025-2026</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">Even Semester (II, IV, VI)</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">84 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">62 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">14 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">8 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">90.47%</td>
                    <td className="px-4 py-3 text-right">
                      <div className="flex items-center justify-end gap-1.5">
                        <button
                          onClick={() => toast.info("Viewing details...")}
                          className="p-1 rounded text-slate-400 hover:text-[#071A4D] hover:bg-slate-100 transition-colors cursor-pointer"
                          title="View Details"
                        >
                          <Eye className="w-3.5 h-3.5" />
                        </button>
                        <button
                          onClick={() => toast.info("Edit record...")}
                          className="p-1 rounded text-slate-400 hover:text-[#071A4D] hover:bg-slate-100 transition-colors cursor-pointer"
                          title="Edit Record"
                        >
                          <Edit2 className="w-3.5 h-3.5" />
                        </button>
                      </div>
                    </td>
                  </tr>
                <tr key={1} className="border-b border-[#E5EAF0] hover:bg-slate-50/80 transition-colors">
                    <td className="px-4 py-3 font-semibold text-[#071A4D] whitespace-nowrap">2025-2026</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">Odd Semester (I, III, V)</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">92 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">70 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">12 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">10 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">89.13%</td>
                    <td className="px-4 py-3 text-right">
                      <div className="flex items-center justify-end gap-1.5">
                        <button
                          onClick={() => toast.info("Viewing details...")}
                          className="p-1 rounded text-slate-400 hover:text-[#071A4D] hover:bg-slate-100 transition-colors cursor-pointer"
                          title="View Details"
                        >
                          <Eye className="w-3.5 h-3.5" />
                        </button>
                        <button
                          onClick={() => toast.info("Edit record...")}
                          className="p-1 rounded text-slate-400 hover:text-[#071A4D] hover:bg-slate-100 transition-colors cursor-pointer"
                          title="Edit Record"
                        >
                          <Edit2 className="w-3.5 h-3.5" />
                        </button>
                      </div>
                    </td>
                  </tr>
                <tr key={2} className="border-b border-[#E5EAF0] hover:bg-slate-50/80 transition-colors">
                    <td className="px-4 py-3 font-semibold text-[#071A4D] whitespace-nowrap">2024-2025</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">Even Semester (II, IV, VI)</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">78 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">58 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">11 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">9 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">88.46%</td>
                    <td className="px-4 py-3 text-right">
                      <div className="flex items-center justify-end gap-1.5">
                        <button
                          onClick={() => toast.info("Viewing details...")}
                          className="p-1 rounded text-slate-400 hover:text-[#071A4D] hover:bg-slate-100 transition-colors cursor-pointer"
                          title="View Details"
                        >
                          <Eye className="w-3.5 h-3.5" />
                        </button>
                        <button
                          onClick={() => toast.info("Edit record...")}
                          className="p-1 rounded text-slate-400 hover:text-[#071A4D] hover:bg-slate-100 transition-colors cursor-pointer"
                          title="Edit Record"
                        >
                          <Edit2 className="w-3.5 h-3.5" />
                        </button>
                      </div>
                    </td>
                  </tr>
                <tr key={3} className="border-b border-[#E5EAF0] hover:bg-slate-50/80 transition-colors">
                    <td className="px-4 py-3 font-semibold text-[#071A4D] whitespace-nowrap">2024-2025</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">Odd Semester (I, III, V)</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">88 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">66 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">13 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">9 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">89.77%</td>
                    <td className="px-4 py-3 text-right">
                      <div className="flex items-center justify-end gap-1.5">
                        <button
                          onClick={() => toast.info("Viewing details...")}
                          className="p-1 rounded text-slate-400 hover:text-[#071A4D] hover:bg-slate-100 transition-colors cursor-pointer"
                          title="View Details"
                        >
                          <Eye className="w-3.5 h-3.5" />
                        </button>
                        <button
                          onClick={() => toast.info("Edit record...")}
                          className="p-1 rounded text-slate-400 hover:text-[#071A4D] hover:bg-slate-100 transition-colors cursor-pointer"
                          title="Edit Record"
                        >
                          <Edit2 className="w-3.5 h-3.5" />
                        </button>
                      </div>
                    </td>
                  </tr>
                <tr key={4} className="border-b border-[#E5EAF0] hover:bg-slate-50/80 transition-colors">
                    <td className="px-4 py-3 font-semibold text-[#071A4D] whitespace-nowrap">2023-2024</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">Annual Academic Cycle</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">164 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">126 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">22 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">16 Cases</td>
                    <td className="px-4 py-3 text-[#334155] text-xs whitespace-nowrap">90.24%</td>
                    <td className="px-4 py-3 text-right">
                      <div className="flex items-center justify-end gap-1.5">
                        <button
                          onClick={() => toast.info("Viewing details...")}
                          className="p-1 rounded text-slate-400 hover:text-[#071A4D] hover:bg-slate-100 transition-colors cursor-pointer"
                          title="View Details"
                        >
                          <Eye className="w-3.5 h-3.5" />
                        </button>
                        <button
                          onClick={() => toast.info("Edit record...")}
                          className="p-1 rounded text-slate-400 hover:text-[#071A4D] hover:bg-slate-100 transition-colors cursor-pointer"
                          title="Edit Record"
                        >
                          <Edit2 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 {filteredRows.length} of {allRows.length} records
          </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>
    </div>
  );
}
