<?php

namespace Tests\Feature;

use App\Models\Account;
use App\Models\ArchiveSetting;
use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ArchiveContractAccountChainTest extends \Tests\TestCase
{
    use RefreshDatabase;

    public function test_archives_account_via_contracts_account_chain(): void
    {
        $case = ClientCase::factory()->create();
        $contract = Contract::factory()->create([
            'client_case_id' => $case->id,
            'active' => 1,
        ]);
        $accountTypeId = \DB::table('account_types')->insertGetId([
            'name' => 'Type',
            'description' => null,
            'created_at' => now(),
            'updated_at' => now(),
            'deleted_at' => null,
        ]);
        $account = Account::create([
            'contract_id' => $contract->id,
            'type_id' => $accountTypeId,
            'active' => 1,
            'initial_amount' => 0,
            'balance_amount' => 0,
        ]);

        ArchiveSetting::factory()->create([
            'enabled' => true,
            'strategy' => 'manual',
            'soft' => true,
            'entities' => [
                ['table' => 'contracts', 'focus' => true],
                ['table' => 'contracts.account'],
            ],
        ]);

        $user = User::factory()->create();
        $this->actingAs($user);

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

        $this->assertDatabaseHas('accounts', ['id' => $account->id, 'active' => 0]);
    }
}
