import React from 'react';
import { NextRequest, NextResponse } from 'next/server';
import { createClient } from '@supabase/supabase-js';
import { generateContractPdf } from '@/lib/pdf/generateContract';
import { ContratCDDUData } from '@/lib/pdf/types';

/**
 * Route API pour tester la génération de PDF d'un contrat existant
 * 
 * URL: GET /api/contrats/[id]/generate-pdf-test
 * 
 * Cette route récupère un contrat depuis Supabase et génère son PDF
 */
export async function GET(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const contractId = params.id;
    console.log('🧪 [Test PDF Generation] Génération du PDF pour le contrat:', contractId);

    // 1. Récupérer les données du contrat depuis Supabase (avec service_role pour bypass RLS)
    const supabase = createClient(
      process.env.NEXT_PUBLIC_SUPABASE_URL!,
      process.env.SUPABASE_SERVICE_ROLE_KEY!,
      {
        auth: {
          autoRefreshToken: false,
          persistSession: false
        }
      }
    );
    
    const { data: contract, error: contractError } = await supabase
      .from('cddu_contracts')
      .select('*')
      .eq('id', contractId)
      .single();

    if (contractError || !contract) {
      console.error('❌ [Test PDF Generation] Contrat non trouvé:', contractError);
      return NextResponse.json(
        { error: 'Contrat non trouvé', details: contractError?.message },
        { status: 404 }
      );
    }

    console.log('✅ [Test PDF Generation] Contrat récupéré:', {
      id: contract.id,
      employee: contract.employee_name,
      organization: contract.structure,
      contract_number: contract.contract_number,
    });

    // 2. Transformer les données au format attendu par le template PDF
    // La table cddu_contracts contient déjà toutes les données nécessaires
    const pdfData: ContratCDDUData = {
      // Structure employeur
      structure_name: contract.structure || '',
      structure_adresse: contract.structure_address || '',
      structure_cpville: contract.structure_postal_code || '',
      structure_ville: contract.structure_city || '',
      structure_siret: contract.structure_siret || '',
      structure_licence: contract.structure_license || '',
      structure_signataire: contract.structure_signatory || '',
      structure_signatairequalite: contract.structure_signatory_title || '',
      structure_spectacle: contract.is_spectacle ? 'Oui' : 'Non',
      delegation: contract.delegation || '',
      forme_juridique: contract.legal_form || '',

      // Représentant légal (mineur)
      mineur1618: contract.is_minor ? 'Oui' : 'Non',
      representant_civ: contract.guardian_civility || '',
      representant_nom: contract.guardian_name || '',
      representant_dob: contract.guardian_birth_date || '',
      representant_cob: contract.guardian_birth_place || '',
      representant_adresse: contract.guardian_address || '',

      // Salarié - utiliser les champs qui existent vraiment dans cddu_contracts
      employee_civ: contract.employee_civility || '',
      employee_firstname: contract.employee_firstname || contract.prenom || '',
      employee_lastname: contract.employee_lastname || contract.nom || '',
      employee_birthname: contract.employee_birthname || contract.nomnaiss || '',
      employee_dob: contract.employee_birth_date || contract.datedenaissancemois || '',
      employee_cob: contract.employee_birth_place || contract.communedenaiss || '',
      employee_address: contract.employee_address || contract.adressepostale || '',
      employee_ss: contract.employee_ss_number || contract.secu || '',
      employee_cs: contract.employee_classification || contract.cs || '',
      employee_profession: contract.employee_profession || contract.profession || '',
      employee_codeprofession: contract.employee_profession_code || contract.codeprofession || '',
      employee_catpro: contract.employee_category || contract.catpro || '',
      employee_pseudo: contract.employee_artistic_name || contract.pseudo || '',

      // Spectacle/Production
      spectacle: contract.production_name || contract.spectacle || '',
      numobjet: contract.object_number || contract.numobjet || '',
      type_numobjet: contract.object_type || contract.typedenumobjet || '',

      // Dates et durée
      date_debut: contract.start_date || contract.datedebutcontrat || '',
      date_fin: contract.end_date || contract.datefincontrat || '',
      dates_travaillees: contract.working_dates || contract.datestravaillees || '',
      date_signature: contract.signature_date || new Date().toISOString().split('T')[0],

      // Rémunération
      salaire_brut: contract.gross_salary?.toString() || contract.brut || '0',
      precisions_salaire: contract.salary_details || contract.precisionssalaire || '',
      panierrepas: contract.meal_allowance || contract.panierrepas || '',
      panierrepasccn: contract.meal_allowance_ccn ? 'Oui' : 'Non',
      montantpanierrepas: contract.meal_allowance_amount?.toString() || contract.montantpanierrepas || '',
      hebergement: contract.accommodation || contract.hebergement || '',
      hebergementccn: contract.accommodation_ccn ? 'Oui' : 'Non',
      montanthebergement: contract.accommodation_amount?.toString() || contract.montanthebergement || '',
      
      // Cachets
      cachets: {
        representations: contract.cachets_representations || contract.representations || 0,
        repetitions: contract.cachets_repetitions || contract.repetitions || 0,
        heures: contract.cachets_hours || contract.heures || 0,
        heuresparjour: contract.cachets_hours_per_day || contract.heuresparjour || 0,
      },
      
      // Convention collective
      CCN: contract.collective_agreement || contract.ccn || contract.conventioncollective || '',

      // Autres
      autreprecision: contract.other_details || contract.autreprecision || '',
      autreprecision_duree: contract.autreprecision_duree || '',
      autreprecision_salaire: contract.autreprecision_salaire || '',
      nom_responsable_traitement: contract.data_controller_name || '',
      qualite_responsable_traitement: contract.data_controller_title || '',
      email_responsable_traitement: contract.data_controller_email || '',
      imageUrl: '', // Pas d'image pour le test
    };

    console.log('📄 [Test PDF Generation] Données transformées, génération du PDF...');

    // 3. Générer le PDF
    const pdfBuffer = await generateContractPdf(pdfData);

    console.log(`✅ [Test PDF Generation] PDF généré avec succès (${pdfBuffer.byteLength} bytes)`);

    // 4. Retourner le PDF
    return new NextResponse(new Uint8Array(pdfBuffer), {
      status: 200,
      headers: {
        'Content-Type': 'application/pdf',
        'Content-Disposition': `inline; filename="contrat_${contractId}.pdf"`,
        'Content-Length': pdfBuffer.byteLength.toString(),
      },
    });

  } catch (error) {
    console.error('❌ [Test PDF Generation] Erreur:', error);
    
    return NextResponse.json(
      {
        error: 'Erreur lors de la génération du PDF',
        details: error instanceof Error ? error.message : String(error),
        stack: error instanceof Error ? error.stack : undefined,
      },
      { status: 500 }
    );
  }
}
