import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import DriverDashboard from '@/components/DriverDashboard';

export default async function DriverPage() {
  const session = await getServerSession(authOptions);

  const myTours = await prisma.tour.findMany({
    where: {
      driverId: session?.user.id,
      tourDate: {
        gte: new Date(),
      },
    },
    include: {
      bookings: {
        include: {
          location: true,
          photobox: true,
        },
      },
    },
    orderBy: {
      tourDate: 'asc',
    },
    take: 10,
  });

  const availableTours = await prisma.tour.findMany({
    where: {
      driverId: null,
      tourDate: {
        gte: new Date(),
      },
    },
    include: {
      bookings: {
        where: {
          status: 'CONFIRMED',
        },
        include: {
          location: true,
        },
      },
    },
    orderBy: {
      tourDate: 'asc',
    },
    take: 10,
  });

  return (
    <DriverDashboard 
      user={session?.user}
      myTours={myTours}
      availableTours={availableTours}
    />
  );
}
