<?php

use App\Models\Activity;
use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\Document;
use App\Models\Segment;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('displays client case with contracts, activities, and documents', function () {
    $user = User::factory()->create();
    $case = ClientCase::factory()->create(['active' => 1]);
    $contract = Contract::factory()->for($case, 'clientCase')->create();
    $activity = Activity::factory()->for($case, 'clientCase')->create();
    $document = Document::factory()->create([
        'documentable_type' => ClientCase::class,
        'documentable_id' => $case->id,
    ]);

    $this->actingAs($user)
        ->get(route('clientCase.show', $case->uuid))
        ->assertSuccessful()
        ->assertInertia(fn ($page) => $page
            ->component('Cases/Show')
            ->has('client_case')
            ->has('contracts.data')
            ->has('activities.data')
            ->has('documents.data')
        );
});

it('filters contracts by segment when segment parameter is provided', function () {
    $user = User::factory()->create();
    $case = ClientCase::factory()->create(['active' => 1]);
    $segment = Segment::factory()->create(['active' => true]);
    
    $contractInSegment = Contract::factory()->for($case, 'clientCase')->create();
    $contractOutSegment = Contract::factory()->for($case, 'clientCase')->create();
    
    // Attach segment to the contract that should appear
    \DB::table('contract_segment')->insert([
        'contract_id' => $contractInSegment->id,
        'segment_id' => $segment->id,
        'active' => true,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $response = $this->actingAs($user)
        ->get(route('clientCase.show', ['client_case' => $case->uuid, 'segment' => $segment->id]))
        ->assertSuccessful();

    $contracts = $response->viewData('page')['props']['contracts']['data'] ?? [];
    expect(collect($contracts)->pluck('id')->contains($contractInSegment->id))->toBeTrue()
        ->and(collect($contracts)->pluck('id')->contains($contractOutSegment->id))->toBeFalse();
});

it('paginates contracts with custom per_page parameter', function () {
    $user = User::factory()->create();
    $case = ClientCase::factory()->create(['active' => 1]);
    
    // Create 60 contracts
    Contract::factory()->count(60)->for($case, 'clientCase')->create();

    $response = $this->actingAs($user)
        ->get(route('clientCase.show', ['client_case' => $case->uuid, 'contracts_per_page' => 25]))
        ->assertSuccessful();

    $meta = $response->viewData('page')['props']['contracts']['meta'] ?? [];
    expect($meta['per_page'])->toBe(25)
        ->and($meta['total'])->toBe(60);
});

it('paginates activities with custom per_page parameter', function () {
    $user = User::factory()->create();
    $case = ClientCase::factory()->create(['active' => 1]);
    
    // Create 30 activities
    Activity::factory()->count(30)->for($case, 'clientCase')->create();

    $response = $this->actingAs($user)
        ->get(route('clientCase.show', ['client_case' => $case->uuid, 'activities_per_page' => 15]))
        ->assertSuccessful();

    $meta = $response->viewData('page')['props']['activities']['meta'] ?? [];
    expect($meta['per_page'])->toBe(15)
        ->and($meta['total'])->toBe(30);
});

it('includes archive metadata in response', function () {
    $user = User::factory()->create();
    $case = ClientCase::factory()->create(['active' => 1]);

    $this->actingAs($user)
        ->get(route('clientCase.show', $case->uuid))
        ->assertSuccessful()
        ->assertInertia(fn ($page) => $page
            ->has('archive_meta')
            ->has('archive_meta.archive_segment_id')
            ->has('archive_meta.related_tables')
        );
});

it('merges case and contract documents correctly', function () {
    $user = User::factory()->create();
    $case = ClientCase::factory()->create(['active' => 1]);
    $contract = Contract::factory()->for($case, 'clientCase')->create();

    $caseDoc = Document::factory()->create([
        'documentable_type' => ClientCase::class,
        'documentable_id' => $case->id,
    ]);

    $contractDoc = Document::factory()->create([
        'documentable_type' => Contract::class,
        'documentable_id' => $contract->id,
    ]);

    $response = $this->actingAs($user)
        ->get(route('clientCase.show', $case->uuid))
        ->assertSuccessful();

    $documents = $response->viewData('page')['props']['documents']['data'] ?? [];
    expect($documents)->toHaveCount(2);
});

