import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.57.4";
import { Novu } from "npm:@novu/api@1.6.0";

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

const TOPICS = {
  MODERATION_SUBMISSIONS: 'moderation-submissions',
  MODERATION_REPORTS: 'moderation-reports',
} as const;

// Simple request tracking
const startRequest = () => ({ requestId: crypto.randomUUID(), start: Date.now() });
const endRequest = (tracking: { start: number }) => Date.now() - tracking.start;

serve(async (req) => {
  const tracking = startRequest();
  
  if (req.method === 'OPTIONS') {
    return new Response(null, { headers: corsHeaders });
  }

  try {
    const novuApiKey = Deno.env.get('NOVU_API_KEY');
    const supabaseUrl = Deno.env.get('SUPABASE_URL');
    const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY');

    if (!novuApiKey || !supabaseUrl || !supabaseServiceKey) {
      throw new Error('Missing required environment variables');
    }

    const novu = new Novu({ secretKey: novuApiKey });
    const supabase = createClient(supabaseUrl, supabaseServiceKey);

    console.log('Starting moderator sync to Novu topics...');

    // Get all moderators, admins, and superusers
    const { data: moderatorRoles, error: rolesError } = await supabase
      .from('user_roles')
      .select('user_id, role')
      .in('role', ['moderator', 'admin', 'superuser']);

    if (rolesError) {
      throw new Error(`Failed to fetch moderator roles: ${rolesError.message}`);
    }

    // Get unique user IDs (a user might have multiple moderator-level roles)
    const uniqueUserIds = [...new Set(moderatorRoles.map(r => r.user_id))];

    console.log(`Found ${uniqueUserIds.length} unique moderators to sync`);

    const topics = [TOPICS.MODERATION_SUBMISSIONS, TOPICS.MODERATION_REPORTS];
    const results = {
      totalUsers: uniqueUserIds.length,
      topics: [] as any[],
    };

    for (const topicKey of topics) {
      try {
        // Ensure topic exists (Novu will create it if it doesn't)
        await novu.topics.create({ key: topicKey, name: topicKey });
        console.log(`Topic ${topicKey} ready`);
      } catch (error: any) {
        // Topic might already exist, which is fine
        if (!error.message?.includes('already exists')) {
          console.warn(`Note about topic ${topicKey}:`, error.message);
        }
      }

      // Add all moderators to the topic in batches
      const batchSize = 100;
      let successCount = 0;
      let errorCount = 0;

      for (let i = 0; i < uniqueUserIds.length; i += batchSize) {
        const batch = uniqueUserIds.slice(i, i + batchSize);
        
        try {
          await novu.topics.addSubscribers(topicKey, {
            subscribers: batch,
          });
          successCount += batch.length;
          console.log(`Added batch of ${batch.length} users to ${topicKey}`);
        } catch (error: any) {
          errorCount += batch.length;
          console.error(`Error adding batch to ${topicKey}:`, error.message);
        }
      }

      results.topics.push({
        topicKey,
        successCount,
        errorCount,
      });
    }

    const duration = endRequest(tracking);
    console.log('Sync completed:', results, { requestId: tracking.requestId, duration });

    return new Response(
      JSON.stringify({
        success: true,
        message: 'Moderator sync completed',
        results,
        requestId: tracking.requestId
      }),
      {
        headers: { 
          ...corsHeaders, 
          'Content-Type': 'application/json',
          'X-Request-ID': tracking.requestId 
        },
        status: 200,
      }
    );
  } catch (error: any) {
    const duration = endRequest(tracking);
    console.error('Error syncing moderators to topics:', error, { requestId: tracking.requestId, duration });
    
    return new Response(
      JSON.stringify({
        success: false,
        error: error.message,
        requestId: tracking.requestId
      }),
      {
        headers: { 
          ...corsHeaders, 
          'Content-Type': 'application/json',
          'X-Request-ID': tracking.requestId 
        },
        status: 500,
      }
    );
  }
});
