<?php

use App\Models\ArchiveSetting;
use App\Models\ClientCase;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

uses(RefreshDatabase::class);

/** @var \Tests\TestCase $this */
it('archives a single contract using archive settings', function () {
    $user = User::factory()->create();
    test()->actingAs($user);

    $case = ClientCase::factory()->create(['active' => 1]);
    $typeId = DB::table('contract_types')->insertGetId([
        'name' => 'Standard',
        'description' => 'Test',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $contract = $case->contracts()->create([
        'uuid' => (string) Str::uuid(),
        'reference' => 'T-TEST',
        'start_date' => now()->toDateString(),
        'end_date' => null,
        'type_id' => $typeId,
        'active' => 1,
    ]);

    ArchiveSetting::factory()->create([
        'entities' => [
            ['table' => 'contracts', 'conditions' => ['where' => ['client_case_id' => $case->id]]],
        ],
        'strategy' => 'immediate',
        'soft' => true,
        'enabled' => true,
    ]);

    test()->post(route('clientCase.contract.archive', [$case->uuid, $contract->uuid]))
        ->assertRedirect();

    $contract->refresh();
    expect($contract->active)->toBe(0);
});
