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

export const GET: APIRoute = async ({ request, url }) => {
  const userId = url.searchParams.get('user_id');
  
  if (!userId) {
    return new Response(JSON.stringify({ 
      success: false, 
      error: 'User ID is required' 
    }), { status: 400 });
  }

  try {
    // First check if user is admin
    const { data: user, error: userError } = await supabase
      .from('users')
      .select('role')
      .eq('id', userId)
      .single();

    if (userError || user?.role !== 'admin') {
      return new Response(JSON.stringify({ 
        success: false, 
        error: 'Access denied. Admin privileges required.' 
      }), { status: 403 });
    }

    // Get the user's pricing profile
    const { data: profile, error: profileError } = await supabase
      .from('custom_pricing_profiles')
      .select('id')
      .eq('user_id', userId)
      .single();

    if (profileError) {
      return new Response(JSON.stringify({ 
        success: true, 
        overrides: [] 
      }), { status: 200 });
    }

    const { data: overrides, error } = await supabase
      .from('event_pricing_overrides')
      .select('*')
      .eq('custom_pricing_profile_id', profile.id)
      .order('created_at', { ascending: false });

    if (error) {

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

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

  } catch (error) {

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

export const POST: APIRoute = async ({ request }) => {
  try {
    const body = await request.json();
    const { 
      event_id, 
      custom_pricing_profile_id, 
      use_custom_stripe_account, 
      override_platform_fees,
      platform_fee_type,
      platform_fee_percentage,
      platform_fee_fixed
    } = body;

    if (!event_id || !custom_pricing_profile_id) {
      return new Response(JSON.stringify({ 
        success: false, 
        error: 'Event ID and pricing profile ID are required' 
      }), { status: 400 });
    }

    const { data: override, error } = await supabase
      .from('event_pricing_overrides')
      .insert({
        event_id,
        custom_pricing_profile_id,
        use_custom_stripe_account: use_custom_stripe_account || false,
        override_platform_fees: override_platform_fees || false,
        platform_fee_type,
        platform_fee_percentage,
        platform_fee_fixed
      })
      .select()
      .single();

    if (error) {

      return new Response(JSON.stringify({ 
        success: false, 
        error: 'Failed to create pricing override' 
      }), { status: 500 });
    }

    return new Response(JSON.stringify({ 
      success: true, 
      override 
    }), { status: 201 });

  } catch (error) {

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