// // app/api/getChallenges/route.ts // import { NextRequest, NextResponse } from 'next/server'; // import supabase from '../../../lib/supabaseClient'; // import type { PostgrestFilterBuilder } from "@supabase/postgrest-js"; // interface Challenge { // challenge_id: string; // validation_count: number; // } // export async function GET(req: NextRequest): Promise { // const { searchParams } = new URL(req.url); // const schoolId = searchParams.get('schoolId'); // const editionId = searchParams.get('editionId'); // if (!schoolId || !editionId) { // return NextResponse.json({ error: 'Missing schoolId or editionId' }, { status: 400 }); // } // try { // // Première requête pour obtenir les classes // const { data: classes, error: classError } = await supabase // .from('class') // .select('id') // .eq('school_id', schoolId) // .eq('edition_id', editionId); // if (classError) { // return NextResponse.json({ error: classError.message }, { status: 500 }); // } // const classIds = classes.map((cls: { id: string }) => cls.id); // // Deuxième requête pour obtenir les défis avec somme de occurence_number // const { data: challenges, error: challengeError } = await supabase // .from('player_challenge') // .select('challenge_id, sum(occurence_number) as validation_count') // .in('class_id', classIds) // .group('challenge_id') // .order('validation_count', { ascending: false }) // .limit(3) // if (challengeError) { // return NextResponse.json({ error: challengeError.message }, { status: 500 }); // } // return NextResponse.json(challenges, { status: 200 }); // } catch (error) { // return NextResponse.json({ error: (error as Error).message }, { status: 500 }); // } // }