"use client";

import { useState } from "react";
import { ChevronLeft, ChevronRight, Calendar as CalendarIcon, Clock, MapPin } from "lucide-react";

// --- Tipe Data Mock ---
type Booking = {
  id: number;
  room: string;
  date: string; // Format YYYY-MM-DD
  startTime: string;
  endTime: string;
  purpose: string;
  status: "Approved" | "Pending";
};

// --- Data Dummy (Mock Data) ---
const mockBookings: Booking[] = [
  {
    id: 1,
    room: "Ruang T-301",
    date: "2026-02-05",
    startTime: "07:30",
    endTime: "10:00",
    purpose: "Kuliah Sistem Kendali",
    status: "Approved",
  },
  {
    id: 2,
    room: "Lab IoT",
    date: "2026-02-05", // Tanggal sama dengan atas
    startTime: "13:00",
    endTime: "15:00",
    purpose: "Praktikum Embedded System",
    status: "Approved",
  },
  {
    id: 3,
    room: "Ruang T-302",
    date: "2026-02-12",
    startTime: "09:00",
    endTime: "11:30",
    purpose: "Seminar Proposal",
    status: "Pending",
  },
  {
    id: 4,
    room: "Lab Kendali",
    date: "2026-02-20",
    startTime: "10:00",
    endTime: "12:00",
    purpose: "Riset Skripsi",
    status: "Approved",
  },
];

export default function CalendarPage() {
  const [currentDate, setCurrentDate] = useState(new Date(2026, 1)); // Februari 2026
  const [selectedDate, setSelectedDate] = useState<string | null>(null);

  // --- Helper Functions ---
  const getDaysInMonth = (year: number, month: number) => new Date(year, month + 1, 0).getDate();
  const getFirstDayOfMonth = (year: number, month: number) => new Date(year, month, 1).getDay();

  const handlePrevMonth = () => {
    setCurrentDate(new Date(currentDate.getFullYear(), currentDate.getMonth() - 1));
    setSelectedDate(null);
  };

  const handleNextMonth = () => {
    setCurrentDate(new Date(currentDate.getFullYear(), currentDate.getMonth() + 1));
    setSelectedDate(null);
  };

  const handleDateClick = (day: number) => {
    const year = currentDate.getFullYear();
    const month = String(currentDate.getMonth() + 1).padStart(2, "0");
    const dateStr = String(day).padStart(2, "0");
    const fullDate = `${year}-${month}-${dateStr}`;
    
    // Toggle seleksi: Jika diklik lagi, tutup detailnya
    if (selectedDate === fullDate) {
      setSelectedDate(null);
    } else {
      setSelectedDate(fullDate);
    }
  };

  // --- Generate Calendar Grid ---
  const year = currentDate.getFullYear();
  const month = currentDate.getMonth();
  const daysInMonth = getDaysInMonth(year, month);
  const firstDay = getFirstDayOfMonth(year, month);
  
  // Array kosong untuk padding hari sebelum tanggal 1
  const emptyDays = Array.from({ length: firstDay }, (_, i) => i);
  // Array tanggal 1 sampai akhir bulan
  const days = Array.from({ length: daysInMonth }, (_, i) => i + 1);

  // Nama Bulan
  const monthNames = [
    "Januari", "Februari", "Maret", "April", "Mei", "Juni",
    "Juli", "Agustus", "September", "Oktober", "November", "Desember"
  ];

  // Filter booking untuk tanggal yang dipilih
  const selectedBookings = mockBookings.filter((b) => b.date === selectedDate);

  return (
    <div className="flex flex-col gap-6 lg:flex-row">
      
      {/* --- KIRI: KALENDER --- */}
      <div className="flex-1 rounded-xl border border-stroke bg-white shadow-sm border-gray-200 dark:bg-boxdark">
        {/* Header Kalender */}
        <div className="flex items-center justify-between border-b border-stroke p-4 border-gray-200 sm:p-6">
          <div className="flex items-center gap-2">
             <div className="h-10 w-10 flex items-center justify-center rounded-full bg-yellow-50 text-yellow-600">
                <CalendarIcon size={20} />
             </div>
             <div>
                <h2 className="text-lg font-bold text-black ">
                  {monthNames[month]} {year}
                </h2>
                <p className="text-xs text-gray-500">Pilih tanggal untuk melihat detail.</p>
             </div>
          </div>
          
          <div className="flex gap-2">
            <button onClick={handlePrevMonth} className="p-2 rounded hover:bg-gray-100 dark:hover:bg-meta-4 transition">
              <ChevronLeft size={20} />
            </button>
            <button onClick={handleNextMonth} className="p-2 rounded hover:bg-gray-100 dark:hover:bg-meta-4 transition">
              <ChevronRight size={20} />
            </button>
          </div>
        </div>

        {/* Grid Kalender */}
        <div className="p-4 sm:p-6">
          {/* Nama Hari */}
          <div className="grid grid-cols-7 mb-2">
            {["Min", "Sen", "Sel", "Rab", "Kam", "Jum", "Sab"].map((d) => (
              <span key={d} className="text-center text-sm font-medium text-gray-500 py-2">
                {d}
              </span>
            ))}
          </div>

          {/* Tanggal */}
          <div className="grid grid-cols-7 gap-2">
            {/* Render Empty Cells */}
            {emptyDays.map((_, i) => (
              <div key={`empty-${i}`} className="h-24 sm:h-32"></div>
            ))}

            {/* Render Date Cells */}
            {days.map((day) => {
              // Format tanggal hari ini untuk pengecekan data
              const currentDayStr = `${year}-${String(month + 1).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
              const hasBooking = mockBookings.some((b) => b.date === currentDayStr);
              const isSelected = selectedDate === currentDayStr;

              return (
                <div
                  key={day}
                  onClick={() => handleDateClick(day)}
                  className={`relative flex h-24 sm:h-32 cursor-pointer flex-col justify-between rounded border p-2 transition hover:bg-gray-50 dark:hover:bg-meta-4 
                    ${isSelected 
                      ? "border-yellow-500 bg-yellow-50 dark:bg-slate-800" 
                      : "border-stroke border-gray-200 bg-white dark:bg-boxdark"
                    }`}
                >
                  <span className={`text-sm font-medium ${isSelected ? "text-yellow-600" : "text-black "}`}>
                    {day}
                  </span>
                  
                  {/* Indikator Booking (Dot / Bar) */}
                  {hasBooking && (
                    <div className="flex flex-col gap-1">
                      {mockBookings
                        .filter(b => b.date === currentDayStr)
                        .slice(0, 2) // Batasi tampilan max 2 baris di grid
                        .map((b, idx) => (
                          <div key={idx} className="truncate rounded-sm bg-blue-100 px-1 py-0.5 text-[10px] font-medium text-blue-700 dark:bg-blue-900 dark:text-blue-100">
                             {b.startTime} - {b.room}
                          </div>
                        ))}
                      {mockBookings.filter(b => b.date === currentDayStr).length > 2 && (
                         <span className="text-[10px] text-gray-400 text-center">+ Lainnya</span>
                      )}
                    </div>
                  )}
                </div>
              );
            })}
          </div>
        </div>
      </div>

      {/* --- KANAN: DETAIL SIDEBAR --- */}
      {selectedDate && (
        <div className="w-full lg:w-80 shrink-0 animate-in fade-in slide-in-from-right duration-300">
          <div className="rounded-xl border border-stroke bg-white p-6 shadow-sm border-gray-200 dark:bg-boxdark">
            <h3 className="mb-4 text-xl font-bold text-black  border-b border-stroke pb-2 border-gray-200">
              Jadwal {selectedDate}
            </h3>

            {selectedBookings.length > 0 ? (
              <div className="flex flex-col gap-4">
                {selectedBookings.map((booking) => (
                  <div key={booking.id} className="rounded-lg border border-gray-100 bg-gray-50 p-4 border-gray-200 bg-gray-50">
                    <div className="flex items-center justify-between mb-2">
                       <span className={`px-2 py-0.5 rounded text-xs font-medium ${
                         booking.status === "Approved" 
                           ? "bg-green-100 text-green-700" 
                           : "bg-yellow-100 text-yellow-700"
                       }`}>
                         {booking.status}
                       </span>
                    </div>
                    
                    <h4 className="font-bold text-black  mb-1">{booking.purpose}</h4>
                    
                    <div className="flex items-center gap-2 text-sm text-gray-500 mt-2">
                       <MapPin size={16} /> {booking.room}
                    </div>
                    <div className="flex items-center gap-2 text-sm text-gray-500 mt-1">
                       <Clock size={16} /> {booking.startTime} - {booking.endTime}
                    </div>
                  </div>
                ))}
              </div>
            ) : (
              <div className="py-10 text-center">
                 <p className="text-gray-400 text-sm">Tidak ada jadwal pada tanggal ini.</p>
                 <button className="mt-4 text-sm font-medium text-yellow-600 hover:underline">
                    + Tambah Jadwal
                 </button>
              </div>
            )}
          </div>
        </div>
      )}

    </div>
  );
}