import { describe, it, expect } from 'vitest';
import { GenerateSchema, CheckLicenceSchema } from '../schemas/generate.schema.js';

const validGeneratePayload = {
  licence_key: 'LAU-ABCD-1234-WXYZ',
  notes: 'Cuisine: robinet qui fuit, joint à refaire.',
  photos: [],
  catalogue: [{ ref: 'PLOMB-001', label: 'Joint robinet', price: 45.0, tva_tx: 10 }],
  type_bien: 'residentiel',
};

describe('GenerateSchema — valid cases', () => {
  it('accepts a complete valid payload', () => {
    const result = GenerateSchema.safeParse(validGeneratePayload);
    expect(result.success).toBe(true);
  });

  it('accepts empty photos array', () => {
    const result = GenerateSchema.safeParse({ ...validGeneratePayload, photos: [] });
    expect(result.success).toBe(true);
  });

  it('accepts all valid type_bien values', () => {
    for (const type of ['residentiel', 'bureau', 'autre']) {
      const result = GenerateSchema.safeParse({ ...validGeneratePayload, type_bien: type });
      expect(result.success).toBe(true);
    }
  });

  it('accepts photos with valid data:image/ prefix', () => {
    const result = GenerateSchema.safeParse({
      ...validGeneratePayload,
      photos: ['data:image/jpeg;base64,/9j/4AAQSkZJRgAB'],
    });
    expect(result.success).toBe(true);
  });
});

describe('GenerateSchema — invalid cases', () => {
  it('rejects licence_key that does not match LAU-XXXX-XXXX-XXXX pattern', () => {
    const result = GenerateSchema.safeParse({
      ...validGeneratePayload,
      licence_key: 'INVALID-KEY',
    });
    expect(result.success).toBe(false);
  });

  it('rejects licence_key with lowercase', () => {
    const result = GenerateSchema.safeParse({
      ...validGeneratePayload,
      licence_key: 'lau-abcd-1234-wxyz',
    });
    expect(result.success).toBe(false);
  });

  it('rejects notes shorter than 10 characters', () => {
    const result = GenerateSchema.safeParse({
      ...validGeneratePayload,
      notes: '9charstr!',
    });
    expect(result.success).toBe(false);
  });

  it('rejects empty notes string', () => {
    const result = GenerateSchema.safeParse({
      ...validGeneratePayload,
      notes: '',
    });
    expect(result.success).toBe(false);
  });

  it('rejects photos array with more than 10 elements', () => {
    const tooManyPhotos = Array.from(
      { length: 11 },
      () => 'data:image/jpeg;base64,abc'
    );
    const result = GenerateSchema.safeParse({
      ...validGeneratePayload,
      photos: tooManyPhotos,
    });
    expect(result.success).toBe(false);
  });

  it('rejects photos containing an element that does not start with data:image/', () => {
    const result = GenerateSchema.safeParse({
      ...validGeneratePayload,
      photos: ['https://example.com/image.jpg'],
    });
    expect(result.success).toBe(false);
  });

  it('rejects type_bien not in the enum', () => {
    const result = GenerateSchema.safeParse({
      ...validGeneratePayload,
      type_bien: 'maison',
    });
    expect(result.success).toBe(false);
  });

  it('rejects catalogue with more than 2000 elements', () => {
    const hugeCatalogue = Array.from({ length: 2001 }, (_, i) => ({
      ref: `REF-${i}`,
      label: `Item ${i}`,
      price: 10,
      tva_tx: 20,
    }));
    const result = GenerateSchema.safeParse({
      ...validGeneratePayload,
      catalogue: hugeCatalogue,
    });
    expect(result.success).toBe(false);
  });

  it('accepts catalogue with up to 2000 elements', () => {
    const largeCatalogue = Array.from({ length: 2000 }, (_, i) => ({
      ref: `REF-${i}`,
      label: `Item ${i}`,
      price: 10,
      tva_tx: 20,
    }));
    const result = GenerateSchema.safeParse({
      ...validGeneratePayload,
      catalogue: largeCatalogue,
    });
    expect(result.success).toBe(true);
  });

  it('rejects missing required field licence_key', () => {
    const { licence_key: _, ...rest } = validGeneratePayload;
    const result = GenerateSchema.safeParse(rest);
    expect(result.success).toBe(false);
  });
});

describe('CheckLicenceSchema', () => {
  it('rejects empty licence_key string', () => {
    const result = CheckLicenceSchema.safeParse({ licence_key: '' });
    expect(result.success).toBe(false);
  });

  it('accepts any non-empty string as licence_key', () => {
    const result = CheckLicenceSchema.safeParse({ licence_key: 'any-string' });
    expect(result.success).toBe(true);
  });

  it('accepts a proper formatted licence key', () => {
    const result = CheckLicenceSchema.safeParse({ licence_key: 'LAU-ABCD-1234-WXYZ' });
    expect(result.success).toBe(true);
  });

  it('rejects missing licence_key field', () => {
    const result = CheckLicenceSchema.safeParse({});
    expect(result.success).toBe(false);
  });
});
