import { describe, it, expect } from 'vitest';
import { generateMaterialEventPDF, generateReservationPDF } from './pdfGenerator';

describe('generateReservationPDF', () => {
  it('should generate a PDF buffer for a complete reservation', async () => {
    const data = {
      titre: 'Réservation Salle TOUCAN - 15/04/2026',
      status: 'soumise',
      createdAt: new Date('2026-03-01'),
      dateSubmission: new Date('2026-03-01'),
      dateTraitement: null,
      commentaireAdmin: null,
      formData: {
        nomAssociation: 'Association Test',
        adresseAssociation: '12 rue de la Paix, 97300 Cayenne',
        communeSiege: 'Cayenne',
        representantLegal: 'Jean Dupont',
        telephoneAssociation: '0694123456',
        emailAssociation: 'test@asso.fr',
        sallesSelectionnees: ['Salle TOUCAN', 'Salle IBIS'],
        motifReservation: 'Assemblée générale annuelle',
        dateReservation: '2026-04-15',
        dateFinReservation: '2026-04-15',
        heureDebut: '09:00',
        heureFin: '17:00',
        nombreParticipants: '50',
        materielNecessaire: 'Tables, chaises, vidéoprojecteur',
        observations: 'Prévoir un accès PMR',
      },
    };

    const buffer = await generateReservationPDF(data);
    expect(buffer).toBeInstanceOf(Buffer);
    expect(buffer.length).toBeGreaterThan(0);
    // Verify PDF header
    expect(buffer.slice(0, 5).toString()).toBe('%PDF-');
  });

  it('should generate a PDF with DSU cadre filled', async () => {
    const data = {
      titre: 'Réservation Salle PELICAN',
      status: 'validee',
      createdAt: new Date('2026-03-01'),
      dateSubmission: new Date('2026-03-01'),
      dateTraitement: new Date('2026-03-10'),
      commentaireAdmin: 'Demande acceptée sous conditions',
      formData: {
        nomAssociation: 'Association Sportive',
        adresseAssociation: '5 avenue des Sports, 97360 Mana',
        communeSiege: 'Mana',
        representantLegal: 'Marie Martin',
        telephoneAssociation: '0694567890',
        emailAssociation: 'sport@asso.fr',
        sallesSelectionnees: ['Salle PELICAN'],
        motifReservation: 'Tournoi de basketball',
        dateReservation: '2026-05-20',
        dateFinReservation: '2026-05-21',
        heureDebut: '08:00',
        heureFin: '20:00',
        nombreParticipants: '100',
        materielNecessaire: 'Sono, tables de score',
        observations: '',
        cadreDSU: {
          dateReception: '2026-03-02',
          avisDSU: 'favorable_avec_reserves',
          conditionsParticulieres: 'Nettoyage obligatoire après utilisation',
          cautionRequise: true,
          montantCaution: '500',
          assuranceRequise: true,
          horairesImposes: '08:00 - 20:00 strictement',
          responsableDSU: 'Pierre Durand',
          dateDecision: '2026-03-10',
          observationsDSU: 'Veiller au respect des horaires de fermeture',
        },
      },
    };

    const buffer = await generateReservationPDF(data);
    expect(buffer).toBeInstanceOf(Buffer);
    expect(buffer.length).toBeGreaterThan(0);
    expect(buffer.slice(0, 5).toString()).toBe('%PDF-');
    // PDF with DSU should be larger than without
    expect(buffer.length).toBeGreaterThan(1000);
  });

  it('should generate a PDF with empty DSU cadre (pending)', async () => {
    const data = {
      titre: 'Réservation DOJO',
      status: 'soumise',
      createdAt: new Date('2026-03-05'),
      formData: {
        nomAssociation: 'Club Judo',
        sallesSelectionnees: ['DOJO'],
        motifReservation: 'Entraînement hebdomadaire',
        dateReservation: '2026-04-01',
        heureDebut: '18:00',
        heureFin: '20:00',
      },
    };

    const buffer = await generateReservationPDF(data);
    expect(buffer).toBeInstanceOf(Buffer);
    expect(buffer.length).toBeGreaterThan(0);
    expect(buffer.slice(0, 5).toString()).toBe('%PDF-');
  });

  it('should handle missing optional fields gracefully', async () => {
    const data = {
      titre: 'Réservation minimale',
      status: 'brouillon',
      createdAt: new Date(),
      formData: {},
    };

    const buffer = await generateReservationPDF(data);
    expect(buffer).toBeInstanceOf(Buffer);
    expect(buffer.length).toBeGreaterThan(0);
    expect(buffer.slice(0, 5).toString()).toBe('%PDF-');
  });
});

describe('generateMaterialEventPDF', () => {
  it('should generate a PDF buffer for an event material request', async () => {
    const data = {
      titre: 'Demande matériel événementiel',
      status: 'validee',
      createdAt: new Date('2026-03-01'),
      dateSubmission: new Date('2026-03-01'),
      dateTraitement: new Date('2026-03-10'),
      commentaireAdmin: 'Accord partiel',
      formData: {
        nomAssociation: 'Association Test',
        commune: 'Kourou',
        direction: 'Sports',
        service: 'Animation',
        demandeurNomPrenom: 'Jean Dupont',
        dateDemande: '2026-03-01',
        dateManifestation: '2026-04-15',
        dateDebutManifestation: '2026-04-15',
        dateFinManifestation: '2026-04-16',
        motifDemande: 'Fête communale',
        datePriseEnCharge: '2026-04-15',
        dateRestitution: '2026-04-19',
        materielsDemandes: { tente3x3: true, podium: true, chapiteau5x5: false, autres: true },
        quantitesDemandees: { tente3x3: '5', podium: '1', chapiteau5x5: '', autres: '2' },
        autreMaterielPrecisions: 'Barrières',
        cadreDSU: {
          dateReception: '2026-03-02',
          responsableDSU: 'Pierre Durand',
          dateDecision: '2026-03-10',
          observationsDSU: 'Prévoir le retrait la veille',
          materielEvent: {
            itemsAccordes: { tente3x3: true, podium: true, chapiteau5x5: false, autres: false },
            quantitesAccordees: { tente3x3: '3', podium: '1', chapiteau5x5: '', autres: '' },
            autresPrecisions: '',
          },
        },
      },
    };

    const buffer = await generateMaterialEventPDF(data);
    expect(buffer).toBeInstanceOf(Buffer);
    expect(buffer.length).toBeGreaterThan(0);
    expect(buffer.slice(0, 5).toString()).toBe('%PDF-');
  });
});
