import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { useTimetable } from "@/hooks/queries";

const days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] as const;
const hours = ["08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00"];

export default function StudentTimetable() {
  const { data: timetableSessions = [], isLoading } = useTimetable();
  if (isLoading) return <div className="flex items-center justify-center min-h-[400px]"><div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary" /></div>;

  const mySessions = timetableSessions.filter(s => ["c1", "c2", "c5", "c8"].includes(s.courseId));

  function getSessionAt(day: string, hour: string) {
    return mySessions.find(s => s.day === day && s.startTime <= hour && s.endTime > hour);
  }

  function isStart(day: string, hour: string) {
    return mySessions.find(s => s.day === day && s.startTime === hour);
  }

  function getSpan(session: (typeof mySessions)[0]) {
    const start = parseInt(session.startTime.split(":")[0]);
    const end = parseInt(session.endTime.split(":")[0]);
    const startMin = parseInt(session.startTime.split(":")[1]);
    const endMin = parseInt(session.endTime.split(":")[1]);
    return (end - start) + (endMin > 0 ? 0.5 : 0) - (startMin > 0 ? 0.5 : 0);
  }

  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-2xl font-bold">Timetable</h1>
        <p className="text-muted-foreground">Your weekly class schedule.</p>
      </div>

      <Card>
        <CardContent className="pt-6 overflow-x-auto">
          <div className="min-w-[700px]">
            <div className="grid grid-cols-6 gap-1">
              <div className="text-xs font-medium text-muted-foreground p-2" />
              {days.map(d => (
                <div key={d} className="text-xs font-semibold text-center p-2 bg-muted rounded">{d}</div>
              ))}
            </div>
            {hours.map(hour => (
              <div key={hour} className="grid grid-cols-6 gap-1 min-h-[48px]">
                <div className="text-xs text-muted-foreground p-2 flex items-start">{hour}</div>
                {days.map(day => {
                  const session = isStart(day, hour);
                  const occupied = getSessionAt(day, hour);
                  if (session) {
                    return (
                      <div
                        key={day}
                        className="rounded-md p-2 text-xs text-white"
                        style={{ backgroundColor: session.color, minHeight: `${getSpan(session) * 48}px` }}
                      >
                        <p className="font-semibold">{session.courseName}</p>
                        <p className="opacity-80">{session.startTime}-{session.endTime}</p>
                        <p className="opacity-70">{session.room}</p>
                      </div>
                    );
                  }
                  if (occupied && !session) return <div key={day} />;
                  return <div key={day} className="border border-dashed border-border/50 rounded-md" />;
                })}
              </div>
            ))}
          </div>
        </CardContent>
      </Card>
    </div>
  );
}
