import { NextRequest, NextResponse } from 'next/server';
import { supabaseAdmin } from '@/lib/odentas-sign/supabase';

/**
 * GET /api/odentas-sign/requests/[id]
 * 
 * Récupère les détails d'une demande de signature
 */
export async function GET(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const requestId = params.id;
    
    // Récupérer la demande avec tous ses signataires et positions
    const { data: signRequest, error: requestError } = await supabaseAdmin
      .from('sign_requests')
      .select(`
        *,
        signers(*),
        sign_positions(*)
      `)
      .eq('id', requestId)
      .single();
    
    if (requestError || !signRequest) {
      return NextResponse.json(
        { error: 'Demande de signature introuvable' },
        { status: 404 }
      );
    }
    
    // Calculer le statut de progression
    const totalSigners = signRequest.signers?.length || 0;
    const signedCount = signRequest.signers?.filter((s: any) => s.signed_at !== null).length || 0;
    
    return NextResponse.json({
      success: true,
      request: {
        id: signRequest.id,
        ref: signRequest.ref,
        title: signRequest.title,
        source_s3_key: signRequest.source_s3_key,
        status: signRequest.status,
        created_at: signRequest.created_at,
        progress: {
          total: totalSigners,
          signed: signedCount,
          percentage: totalSigners > 0 ? Math.round((signedCount / totalSigners) * 100) : 0,
        },
      },
      signers: signRequest.signers?.map((s: any) => ({
        id: s.id,
        role: s.role,
        name: s.name,
        email: s.email,
        signed_at: s.signed_at,
        signature_image_s3: s.signature_image_s3,
      })) || [],
      positions: signRequest.sign_positions || [],
    });
    
  } catch (error) {
    console.error('[GET REQUEST] Erreur:', error);
    return NextResponse.json(
      { error: 'Erreur serveur', details: error instanceof Error ? error.message : String(error) },
      { status: 500 }
    );
  }
}
