<?php

use App\Models\Client;
use App\Models\Contract;
use App\Models\Import;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

uses(RefreshDatabase::class);

it('reuses a single person per client_ref (client_case) across multiple rows', function () {
    \Illuminate\Support\Facades\Artisan::call('db:seed', ['--force' => true]);
    $user = User::factory()->create();
    Auth::login($user);

    $client = Client::factory()->create();
    Storage::fake('local');
    $csv = "client_ref,contract.reference,person.first_name\nCASE-PR-1,REF-100,Ana\nCASE-PR-1,REF-200,Borut\n";
    Storage::disk('local')->put('imports/ppl.csv', $csv);

    $import = Import::create([
        'uuid' => (string) Str::uuid(),
        'user_id' => $user->id,
        'client_id' => $client->id,
        'source_type' => 'csv',
        'file_name' => 'ppl.csv',
        'original_name' => 'ppl.csv',
        'disk' => 'local',
        'path' => 'imports/ppl.csv',
        'status' => 'queued',
        'meta' => ['has_header' => true, 'columns' => ['client_ref', 'contract.reference', 'person.first_name']],
        'import_template_id' => null,
    ]);

    // Map client_ref, contract.reference, and a person field
    DB::table('import_mappings')->insert([
        'import_id' => $import->id,
        'source_column' => 'client_ref',
        'target_field' => 'client_case.client_ref',
        'transform' => 'trim|upper',
        'apply_mode' => 'both',
        'options' => null,
        'position' => 0,
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    DB::table('import_mappings')->insert([
        'import_id' => $import->id,
        'source_column' => 'contract.reference',
        'target_field' => 'contract.reference',
        'transform' => 'trim|upper|ref',
        'apply_mode' => 'both',
        'options' => null,
        'position' => 1,
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    DB::table('import_mappings')->insert([
        'import_id' => $import->id,
        'source_column' => 'person.first_name',
        'target_field' => 'person.first_name',
        'transform' => 'trim',
        'apply_mode' => 'both',
        'options' => null,
        'position' => 2,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $service = app(\App\Services\ImportProcessor::class);
    $service->process($import, $user);

    $case = \App\Models\ClientCase::where('client_id', $client->id)->where('client_ref', 'CASE-PR-1')->first();
    expect($case)->not->toBeNull();
    expect($case->person_id)->not->toBeNull();

    // Both contracts should be under the same case
    $countContracts = Contract::where('client_case_id', $case->id)->count();
    expect($countContracts)->toBe(2);

    // The person assigned to the case should be the only person linked for this case usage
    $personId = $case->person_id;
    // There should not be any other person created solely due to these rows; we assert that attaching contacts is done to the same person
    // To be conservative, assert that person exists and is used; deeper global uniqueness is not enforced here.
    expect($personId)->toBeInt();
});
