<?php

use App\Models\Account;
use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\Email;
use App\Models\Import;
use App\Models\Person\AddressType;
use App\Models\Person\Person;
use App\Models\Person\PersonAddress;
use App\Models\Person\PersonPhone;
use App\Models\Person\PhoneType;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

it('attaches chain entities (client_case, person, email, phone, address) for existing contract', function () {
    Storage::fake('local');

    $user = User::factory()->create();
    \Illuminate\Support\Facades\Auth::login($user);
    $person = Person::factory()->create(['user_id' => $user->id]);
    $clientCase = ClientCase::factory()->create(['person_id' => $person->id, 'client_ref' => 'CC-1']);
    $contract = Contract::factory()->create(['reference' => 'C-CHAIN', 'client_case_id' => $clientCase->id]);
    $typeId = DB::table('account_types')->insertGetId(['name' => 'Current', 'description' => 'Test', 'created_at' => now(), 'updated_at' => now()]);
    Account::create(['reference' => 'AC-1', 'contract_id' => $contract->id, 'type_id' => $typeId]);
    Email::factory()->create(['person_id' => $person->id, 'value' => 'chain@example.com']);
    $phoneType = PhoneType::factory()->create();
    $addressType = AddressType::factory()->create();
    PersonPhone::factory()->create(['person_id' => $person->id, 'nu' => '123456', 'type_id' => $phoneType->id]);
    PersonAddress::factory()->create(['person_id' => $person->id, 'address' => 'Main St 1', 'country' => 'SI', 'type_id' => $addressType->id]);

    $csv = "contract_ref\nC-CHAIN";
    Storage::disk('local')->put('imports/chain.csv', $csv);

    $import = Import::factory()->create([
        'disk' => 'local',
        'path' => 'imports/chain.csv',
        'meta' => ['has_header' => true, 'forced_delimiter' => ','],
    ]);

    // Map only contract.reference (others should be derived via chain)
    DB::table('import_mappings')->insert([
        ['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'contract.reference', 'position' => 1],
        // declare roots so they appear in simulation (simulate mapping presence with dummy value fields referencing same column) optional
        ['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'person.first_name', 'position' => 2],
        ['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'client_case.title', 'position' => 3],
        ['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'email.value', 'position' => 4],
        ['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'phone.nu', 'position' => 5],
        ['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'address.address', 'position' => 6],
    ]);

    $service = app(\App\Services\ImportSimulationService::class);
    $result = $service->simulate($import, 10, false);

    $row = $result['rows'][0]['entities'];

    expect($row['contract']['action'])->toBe('update');
    expect($row['client_case']['existing_chain'] ?? false)->toBeTrue();
    expect($row['person']['existing_chain'] ?? false)->toBeTrue();
    expect($row['email']['existing_chain'] ?? false)->toBeTrue();
    expect($row['phone']['existing_chain'] ?? false)->toBeTrue();
    expect($row['address']['existing_chain'] ?? false)->toBeTrue();
});
