<?php

use App\Models\Client;
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('imports account.initial_amount when mapped', 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 with straightforward decimal to avoid delimiter/locale complications
    $csv = "contract.reference,account.reference,account.initial_amount\nREF-IA-1,ACC-IA-1,1234.56\n";
    Storage::disk('local')->put('imports/acc_init.csv', $csv);

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

    // Mappings
    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' => 0,
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    DB::table('import_mappings')->insert([
        'import_id' => $import->id,
        'source_column' => 'contract.reference',
        'target_field' => 'account.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' => 'account.reference',
        'target_field' => 'account.reference',
        'transform' => 'trim|upper|ref',
        'apply_mode' => 'both',
        'options' => null,
        'position' => 2,
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    DB::table('import_mappings')->insert([
        'import_id' => $import->id,
        'source_column' => 'account.initial_amount',
        'target_field' => 'account.initial_amount',
        'transform' => 'trim',
        'apply_mode' => 'both',
        'options' => null,
        'position' => 3,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

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

    $acc = \App\Models\Account::query()->first();
    expect($acc)->not->toBeNull();
    // normalized decimal should be 1234.56
    expect((string) $acc->initial_amount)->toBe('1234.56');
});
