export const prerender = false;

import type { APIRoute } from 'astro';
import { supabase } from '../../../lib/supabase';

export const GET: APIRoute = async ({ params }) => {
  try {
    const eventId = params.eventId;

    if (!eventId) {
      return new Response(JSON.stringify({ 
        success: false, 
        error: 'Event ID is required',
        debug: {
          params: params,
          eventId: eventId
        }
      }), { status: 400 });
    }

    const { data: tickets, error } = await supabase
      .from('printed_tickets')
      .select(`
        *,
        ticket_types (
          name,
          price
        ),
        events (
          title
        )
      `)
      .eq('event_id', eventId)
      .order('created_at', { ascending: false });

    if (error) {

      return new Response(JSON.stringify({ 
        success: false, 
        error: 'Failed to fetch printed tickets' 
      }), { status: 500 });
    }

    return new Response(JSON.stringify({ 
      success: true, 
      tickets: tickets || []
    }), { status: 200 });

  } catch (error) {

    return new Response(JSON.stringify({ 
      success: false, 
      error: 'Internal server error' 
    }), { status: 500 });
  }
};