<?php

use App\Models\Client;
use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\Permission;
use App\Models\Person\Person;
use App\Models\Person\PersonPhone;
use App\Models\Role;
use App\Models\Segment;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;

uses(RefreshDatabase::class);

it('can filter contracts by start date range', function () {
    // Create and authenticate admin user with manage-settings permission
    $user = User::factory()->create();
    $adminRole = Role::firstOrCreate(['slug' => 'admin'], ['name' => 'Admin']);
    $permission = Permission::firstOrCreate(['slug' => 'manage-settings'], ['name' => 'Manage Settings']);
    $adminRole->permissions()->syncWithoutDetaching([$permission->id]);
    DB::table('role_user')->insert([
        'role_id' => $adminRole->id,
        'user_id' => $user->id,
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    $this->actingAs($user);
    // Create a segment
    $segment = Segment::factory()->create(['active' => true]);

    // Create a person with phone
    $person = Person::factory()->create();
    $phone = PersonPhone::factory()->create([
        'person_id' => $person->id,
        'phone_type' => 'mobile',
        'validated' => true,
    ]);

    // Create a client
    $client = Client::factory()->create(['person_id' => $person->id]);

    // Create a client case
    $clientCase = ClientCase::factory()->create([
        'client_id' => $client->id,
        'person_id' => $person->id,
    ]);

    // Create contracts with different start dates
    $contract1 = Contract::factory()->create([
        'client_case_id' => $clientCase->id,
        'start_date' => '2024-01-15',
        'reference' => 'CONTRACT-2024-001',
    ]);

    $contract2 = Contract::factory()->create([
        'client_case_id' => $clientCase->id,
        'start_date' => '2024-03-20',
        'reference' => 'CONTRACT-2024-002',
    ]);

    $contract3 = Contract::factory()->create([
        'client_case_id' => $clientCase->id,
        'start_date' => '2024-05-10',
        'reference' => 'CONTRACT-2024-003',
    ]);

    // Attach contracts to segment
    $contract1->segments()->attach($segment->id, ['active' => true]);
    $contract2->segments()->attach($segment->id, ['active' => true]);
    $contract3->segments()->attach($segment->id, ['active' => true]);

    // Test without date filters - should return all contracts
    $response = $this->getJson(route('packages.contracts', [
        'segment_id' => $segment->id,
    ]));

    $response->assertSuccessful();
    $data = $response->json('data');
    expect($data)->toHaveCount(3);

    // Test with start_date_from filter
    $response = $this->getJson(route('packages.contracts', [
        'segment_id' => $segment->id,
        'start_date_from' => '2024-02-01',
    ]));

    $response->assertSuccessful();
    $data = $response->json('data');
    expect($data)->toHaveCount(2);
    expect(collect($data)->pluck('reference'))->toContain('CONTRACT-2024-002', 'CONTRACT-2024-003');

    // Test with start_date_to filter
    $response = $this->getJson(route('packages.contracts', [
        'segment_id' => $segment->id,
        'start_date_to' => '2024-03-31',
    ]));

    $response->assertSuccessful();
    $data = $response->json('data');
    expect($data)->toHaveCount(2);
    expect(collect($data)->pluck('reference'))->toContain('CONTRACT-2024-001', 'CONTRACT-2024-002');

    // Test with both date filters
    $response = $this->getJson(route('packages.contracts', [
        'segment_id' => $segment->id,
        'start_date_from' => '2024-02-01',
        'start_date_to' => '2024-04-30',
    ]));

    $response->assertSuccessful();
    $data = $response->json('data');
    expect($data)->toHaveCount(1);
    expect($data[0]['reference'])->toBe('CONTRACT-2024-002');
    expect($data[0]['start_date'])->toBe('2024-03-20');
});

it('validates date filter parameters', function () {
    // Create and authenticate admin user with manage-settings permission
    $user = User::factory()->create();
    $adminRole = Role::firstOrCreate(['slug' => 'admin'], ['name' => 'Admin']);
    $permission = Permission::firstOrCreate(['slug' => 'manage-settings'], ['name' => 'Manage Settings']);
    $adminRole->permissions()->syncWithoutDetaching([$permission->id]);
    DB::table('role_user')->insert([
        'role_id' => $adminRole->id,
        'user_id' => $user->id,
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    $this->actingAs($user);

    $segment = Segment::factory()->create(['active' => true]);

    // Test invalid start_date_from
    $response = $this->getJson(route('packages.contracts', [
        'segment_id' => $segment->id,
        'start_date_from' => 'invalid-date',
    ]));

    $response->assertStatus(422);
    $response->assertJsonValidationErrors('start_date_from');

    // Test invalid start_date_to
    $response = $this->getJson(route('packages.contracts', [
        'segment_id' => $segment->id,
        'start_date_to' => 'invalid-date',
    ]));

    $response->assertStatus(422);
    $response->assertJsonValidationErrors('start_date_to');
});
