import { serve } from 'https://deno.land/std@0.168.0/http/server.ts';
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.39.3';
import { Webhook } from 'https://esm.sh/svix@1.15.0';

const CLERK_WEBHOOK_SECRET = Deno.env.get('CLERK_WEBHOOK_SECRET')!;

const corsHeaders = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type, svix-id, svix-signature, svix-timestamp',
};

serve(async (req) => {
  // Handle CORS
  if (req.method === 'OPTIONS') {
    return new Response(null, { headers: corsHeaders });
  }

  const payload = await req.text();
  const headers = Object.fromEntries(req.headers);

  const wh = new Webhook(CLERK_WEBHOOK_SECRET);
  let evt: any;

  try {
    evt = wh.verify(payload, headers);
  } catch (err) {
    console.error('Webhook verification failed:', err);
    return new Response(JSON.stringify({ error: 'Invalid signature' }), {
      status: 400,
      headers: { ...corsHeaders, 'Content-Type': 'application/json' }
    });
  }

  const supabase = createClient(
    Deno.env.get('SUPABASE_URL')!,
    Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
  );

  const eventType = evt.type;
  const { id, email_addresses, username, image_url, created_at, first_name, last_name, unsafe_metadata } = evt.data;

  console.log(`Handling event ${eventType} for user ${id}`);

  if (eventType === 'user.created') {
    const email = email_addresses?.[0]?.email_address;
    const role = unsafe_metadata?.role || 'user';
    
    const { error } = await supabase.from('profiles').insert({
      clerk_user_id: id,
      email: email,
      username: username || email?.split('@')[0] || `user_${id.slice(-6)}`,
      full_name: `${first_name || ''} ${last_name || ''}`.trim() || null,
      avatar_url: image_url,
      role: role,
      created_at: new Date(created_at).toISOString(),
      updated_at: new Date(created_at).toISOString(),
      is_active: true
    });

    if (error) {
      console.error('Profile creation error:', error);
      return new Response(JSON.stringify({ error: 'Profile creation failed', details: error }), {
        status: 500,
        headers: { ...corsHeaders, 'Content-Type': 'application/json' }
      });
    }
  }

  if (eventType === 'user.updated') {
    const email = email_addresses?.[0]?.email_address;
    const { error } = await supabase
      .from('profiles')
      .update({
        email: email,
        username: username || email?.split('@')[0],
        full_name: `${first_name || ''} ${last_name || ''}`.trim() || null,
        avatar_url: image_url,
        updated_at: new Date().toISOString()
      })
      .eq('clerk_user_id', id);

    if (error) {
      console.error('Profile update error:', error);
    }
  }

  if (eventType === 'user.deleted') {
    const { error } = await supabase
      .from('profiles')
      .delete()
      .eq('clerk_user_id', id);

    if (error) {
      console.error('Profile deletion error:', error);
    }
  }

  return new Response(JSON.stringify({ success: true }), {
    status: 200,
    headers: { ...corsHeaders, 'Content-Type': 'application/json' }
  });
});
