<?php

it('validates required fields for event registration', function () {
    $response = $this->postJson('/api/v1/form-event-registration', []);

    $response->assertStatus(422);
    $response->assertJsonValidationErrors([
        'eventRegistration',
        'fullName',
        'jobTitle',
        'department',
        'email',
        'companyName',
        'companyType',
        'specialRequest',
        'dinnerJoin',
        'companyFullName',
        'companyAddress',
        'companyCountry',
        'companyVat',
        'invoiceEmail',
        'additionalInformation',
        'agreePhotos',
        'agreeCheck',
    ]);
});

it('validates email format', function () {
    $response = $this->postJson('/api/v1/form-event-registration', [
        'email' => 'invalid-email',
        'invoiceEmail' => 'also-invalid',
    ]);

    $response->assertStatus(422);
    $response->assertJsonValidationErrors(['email', 'invoiceEmail']);
});

it('validates boolean fields', function () {
    $response = $this->postJson('/api/v1/form-event-registration', [
        'eventRegistration' => 'not-a-boolean',
        'specialRequest' => 'not-a-boolean',
        'additionalInformation' => 'not-a-boolean',
        'agreePhotos' => 'not-a-boolean',
        'agreeCheck' => 'not-a-boolean',
    ]);

    $response->assertStatus(422);
    $response->assertJsonValidationErrors([
        'eventRegistration',
        'specialRequest',
        'additionalInformation',
        'agreePhotos',
        'agreeCheck',
    ]);
});

it('accepts valid event registration data', function () {
    $response = $this->postJson('/api/v1/form-event-registration', [
        'eventRegistration' => true,
        'fullName' => 'John Doe',
        'jobTitle' => 'Energy Trader',
        'department' => 'Trading',
        'email' => 'john@example.com',
        'companyName' => 'Energy Corp',
        'companyType' => 'Trading Company',
        'specialRequest' => false,
        'dinnerJoin' => 'yes',
        'companyFullName' => 'Energy Corporation Ltd',
        'companyAddress' => '123 Energy Street, Brussels',
        'companyCountry' => 'Belgium',
        'companyVat' => 'BE0123456789',
        'invoiceEmail' => 'invoices@example.com',
        'additionalInformation' => false,
        'agreePhotos' => true,
        'agreeCheck' => true,
        'tokenRecaptcha' => 'test-token',
    ]);

    // Will fail due to recaptcha, but validation should pass
    expect($response->status())->toBeIn([200, 422]);
});

it('validates maximum field lengths', function () {
    $longString = str_repeat('a', 300);

    $response = $this->postJson('/api/v1/form-event-registration', [
        'eventRegistration' => true,
        'fullName' => $longString,
        'jobTitle' => 'Trader',
        'department' => 'Trading',
        'email' => 'john@example.com',
        'companyName' => 'Company',
        'companyType' => 'Type',
        'specialRequest' => false,
        'dinnerJoin' => 'yes',
        'companyFullName' => 'Company Ltd',
        'companyAddress' => '123 Street',
        'companyCountry' => 'Belgium',
        'companyVat' => 'BE123',
        'invoiceEmail' => 'invoice@example.com',
        'additionalInformation' => false,
        'agreePhotos' => true,
        'agreeCheck' => true,
        'tokenRecaptcha' => 'test-token',
    ]);

    $response->assertStatus(422);
    $response->assertJsonValidationErrors(['fullName']);
});

it('accepts optional fields when provided', function () {
    $response = $this->postJson('/api/v1/form-event-registration', [
        'eventRegistration' => true,
        'fullName' => 'John Doe',
        'jobTitle' => 'Trader',
        'department' => 'Trading',
        'email' => 'john@example.com',
        'companyName' => 'Company',
        'companyType' => 'Type',
        'specialRequest' => true,
        'specialRequestComment' => 'Vegetarian meal please', // Optional
        'dinnerJoin' => 'yes',
        'companyFullName' => 'Company Ltd',
        'companyAddress' => '123 Street',
        'companyCountry' => 'Belgium',
        'companyVat' => 'BE123',
        'invoiceEmail' => 'invoice@example.com',
        'orderNumber' => 'PO-12345', // Optional
        'additionalInformation' => true,
        'additionalInformationComment' => 'Need parking access', // Optional
        'agreePhotos' => true,
        'agreeCheck' => true,
        'tokenRecaptcha' => 'test-token',
    ]);

    // Optional fields should not cause validation errors
    $errors = $response->json('errors') ?? [];
    expect($errors)->not->toHaveKey('specialRequestComment');
    expect($errors)->not->toHaveKey('orderNumber');
    expect($errors)->not->toHaveKey('additionalInformationComment');
});

it('handles null optional fields', function () {
    $response = $this->postJson('/api/v1/form-event-registration', [
        'eventRegistration' => true,
        'fullName' => 'John Doe',
        'jobTitle' => 'Trader',
        'department' => 'Trading',
        'email' => 'john@example.com',
        'companyName' => 'Company',
        'companyType' => 'Type',
        'specialRequest' => false,
        'specialRequestComment' => null,
        'dinnerJoin' => 'no',
        'companyFullName' => 'Company Ltd',
        'companyAddress' => '123 Street',
        'companyCountry' => 'Belgium',
        'companyVat' => 'BE123',
        'invoiceEmail' => 'invoice@example.com',
        'orderNumber' => null,
        'additionalInformation' => false,
        'additionalInformationComment' => null,
        'agreePhotos' => true,
        'agreeCheck' => true,
        'tokenRecaptcha' => 'test-token',
    ]);

    // Null optional fields should not cause validation errors
    $errors = $response->json('errors') ?? [];
    expect($errors)->not->toHaveKey('specialRequestComment');
    expect($errors)->not->toHaveKey('orderNumber');
    expect($errors)->not->toHaveKey('additionalInformationComment');
});

it('sanitizes XSS in registration data', function () {
    $response = $this->postJson('/api/v1/form-event-registration', [
        'eventRegistration' => true,
        'fullName' => '<script>alert("xss")</script>John',
        'jobTitle' => 'Trader<img onerror="alert(1)" src="x">',
        'department' => 'Trading',
        'email' => 'john@example.com',
        'companyName' => 'Company',
        'companyType' => 'Type',
        'specialRequest' => false,
        'dinnerJoin' => 'yes',
        'companyFullName' => 'Company Ltd',
        'companyAddress' => '123 Street',
        'companyCountry' => 'Belgium',
        'companyVat' => 'BE123',
        'invoiceEmail' => 'invoice@example.com',
        'additionalInformation' => false,
        'agreePhotos' => true,
        'agreeCheck' => true,
        'tokenRecaptcha' => 'test-token',
    ]);

    // XSS should be sanitized
    expect($response->status())->toBeIn([200, 422]);
});
