import { prisma } from "@/infrastructure/db/prisma";
import { ScheduleCalendar } from "@/components/ScheduleCalendar";
import Link from "next/link";
import styles from "./page.module.css";

export default async function Home() {
  // Calculate start and end of current week
  const now = new Date();
  const dayOfWeek = now.getDay(); // 0 is Sunday
  const diff = now.getDate() - dayOfWeek + (dayOfWeek === 0 ? -6 : 1); // Adjust to Monday
  const startOfWeek = new Date(now.setDate(diff));
  startOfWeek.setHours(0, 0, 0, 0);

  const endOfWeek = new Date(startOfWeek);
  endOfWeek.setDate(endOfWeek.getDate() + 7);

  const lessons = await prisma.lesson.findMany({
    where: {
      startTime: {
        gte: startOfWeek,
        lt: endOfWeek,
      },
    },
    include: {
      instructor: true,
      branch: true,
      class: true,
    },
    orderBy: {
      startTime: 'asc',
    },
  });

  return (
    <div className={styles.page}>
      <nav className={styles.nav}>
        <div className={styles.logo}>Dance School</div>
        <Link href="/admin/dashboard" className={styles.adminLink}>
          Yönetici Paneli
        </Link>
      </nav>

      <main className={styles.main}>
        <section className={styles.hero}>
          <h1>Dansın Ritmini Keşfedin</h1>
          <p>Haftalık kurs programımızı aşağıdan takip edebilirsiniz.</p>
        </section>

        <ScheduleCalendar lessons={lessons} />
      </main>

      <footer className={styles.footer}>
        <p>&copy; {new Date().getFullYear()} Dance School Yönetim Sistemi. Tüm hakları saklıdır.</p>
      </footer>
    </div>
  );
}
