export const prerender = false;

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

export const GET: APIRoute = async ({ params }) => {
  const { userId } = params;

  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 });
    }

    const { data: profile, error } = await supabase
      .from('custom_pricing_profiles')
      .select('*')
      .eq('user_id', userId)
      .single();

    if (error && error.code !== 'PGRST116') {

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

    // If no profile exists, create one
    if (!profile) {
      const { data: newProfile, error: createError } = await supabase
        .from('custom_pricing_profiles')
        .insert({
          user_id: userId,
          can_override_pricing: true,
          can_set_custom_fees: true,
          use_personal_stripe: false
        })
        .select()
        .single();

      if (createError) {

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

      return new Response(JSON.stringify({ 
        success: true, 
        profile: newProfile 
      }), { status: 200 });
    }

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

  } catch (error) {

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

export const PUT: APIRoute = async ({ params, request }) => {
  const { userId } = params;

  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 });
    }

    const body = await request.json();
    const updateData = {
      ...body,
      updated_at: new Date().toISOString()
    };

    const { data: profile, error } = await supabase
      .from('custom_pricing_profiles')
      .update(updateData)
      .eq('user_id', userId)
      .select()
      .single();

    if (error) {

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

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

  } catch (error) {

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