<?php

use App\Models\Client;
use App\Models\User;
use App\Models\Workspace;

test('can create vat_monthly declaration requiring month', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create();
    $workspace->users()->attach($user, ['role' => 'owner']);
    $client = Client::factory()->create(['workspace_id' => $workspace->id]);
    session(['current_workspace_id' => $workspace->id]);

    $response = $this->actingAs($user)->post(route('declarations.store'), [
        'client_id' => $client->id,
        'title' => 'TVA Mensuelle Mars 2026',
        'type' => 'vat_monthly',
        'period_year' => 2026,
        'period_month' => 3,
        'status' => 'created',
        'priority' => 'medium',
    ]);

    $response->assertRedirect();
    $declaration = $client->declarations()->where('type', 'vat_monthly')->first();
    expect($declaration)->not->toBeNull();
    expect($declaration->period_month)->toBe(3);
    expect($declaration->period_quarter)->toBeNull();
});

test('vat_monthly validation fails without month', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create();
    $workspace->users()->attach($user, ['role' => 'owner']);
    $client = Client::factory()->create(['workspace_id' => $workspace->id]);
    session(['current_workspace_id' => $workspace->id]);

    $response = $this->actingAs($user)->post(route('declarations.store'), [
        'client_id' => $client->id,
        'title' => 'TVA Mensuelle Sans Mois',
        'type' => 'vat_monthly',
        'period_year' => 2026,
        'status' => 'created',
        'priority' => 'medium',
    ]);

    $response->assertSessionHasErrors('period_month');
});

test('can create vat_quarterly declaration requiring quarter', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create();
    $workspace->users()->attach($user, ['role' => 'owner']);
    $client = Client::factory()->create(['workspace_id' => $workspace->id]);
    session(['current_workspace_id' => $workspace->id]);

    $response = $this->actingAs($user)->post(route('declarations.store'), [
        'client_id' => $client->id,
        'title' => 'TVA Trimestrielle T1 2026',
        'type' => 'vat_quarterly',
        'period_year' => 2026,
        'period_quarter' => 1,
        'status' => 'created',
        'priority' => 'medium',
    ]);

    $response->assertRedirect();
    $declaration = $client->declarations()->where('type', 'vat_quarterly')->first();
    expect($declaration)->not->toBeNull();
    expect($declaration->period_quarter)->toBe(1);
    expect($declaration->period_month)->toBeNull();
});

test('vat_quarterly validation fails without quarter', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create();
    $workspace->users()->attach($user, ['role' => 'owner']);
    $client = Client::factory()->create(['workspace_id' => $workspace->id]);
    session(['current_workspace_id' => $workspace->id]);

    $response = $this->actingAs($user)->post(route('declarations.store'), [
        'client_id' => $client->id,
        'title' => 'TVA Trimestrielle Sans Trimestre',
        'type' => 'vat_quarterly',
        'period_year' => 2026,
        'status' => 'created',
        'priority' => 'medium',
    ]);

    $response->assertSessionHasErrors('period_quarter');
});

test('server rejects old vat type', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create();
    $workspace->users()->attach($user, ['role' => 'owner']);
    $client = Client::factory()->create(['workspace_id' => $workspace->id]);
    session(['current_workspace_id' => $workspace->id]);

    $response = $this->actingAs($user)->post(route('declarations.store'), [
        'client_id' => $client->id,
        'title' => 'Old VAT',
        'type' => 'vat',
        'period_year' => 2026,
        'status' => 'created',
        'priority' => 'medium',
    ]);

    $response->assertSessionHasErrors('type');
});

test('vat_monthly nulls quarter field server-side', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create();
    $workspace->users()->attach($user, ['role' => 'owner']);
    $client = Client::factory()->create(['workspace_id' => $workspace->id]);
    session(['current_workspace_id' => $workspace->id]);

    $response = $this->actingAs($user)->post(route('declarations.store'), [
        'client_id' => $client->id,
        'title' => 'TVA Mensuelle With Quarter',
        'type' => 'vat_monthly',
        'period_year' => 2026,
        'period_month' => 6,
        'period_quarter' => 2,
        'status' => 'created',
        'priority' => 'medium',
    ]);

    $response->assertRedirect();
    $declaration = $client->declarations()->where('title', 'TVA Mensuelle With Quarter')->first();
    expect($declaration->period_quarter)->toBeNull();
    expect($declaration->period_month)->toBe(6);
});
