import { NextRequest, NextResponse } from 'next/server';
import { dailyService } from '@/services/task-management/daily';
import { DailyCheckboxType } from '@/lib/types';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/lib/auth';

export async function GET(request: NextRequest) {
  try {
    // Vérifier l'authentification
    const session = await getServerSession(authOptions);
    if (!session?.user?.id) {
      return NextResponse.json({ error: 'Non authentifié' }, { status: 401 });
    }

    const { searchParams } = new URL(request.url);

    const maxDays = searchParams.get('maxDays')
      ? parseInt(searchParams.get('maxDays')!)
      : undefined;
    const excludeToday = searchParams.get('excludeToday') === 'true';
    const type = searchParams.get('type') as DailyCheckboxType | undefined;
    const limit = searchParams.get('limit')
      ? parseInt(searchParams.get('limit')!)
      : undefined;

    const pendingCheckboxes = await dailyService.getPendingCheckboxes({
      maxDays,
      excludeToday,
      type,
      limit,
      userId: session.user.id, // Filtrer par user connecté
    });

    return NextResponse.json(pendingCheckboxes);
  } catch (error) {
    console.error('Error fetching pending checkboxes:', error);
    return NextResponse.json(
      { error: 'Failed to fetch pending checkboxes' },
      { status: 500 }
    );
  }
}
