<?php

use App\Models\Company;
use App\Models\Transaction;
use App\Models\User;
use Illuminate\Support\Carbon;
use Livewire\Volt\Volt;

test('guests are redirected to the login page', function () {
    $response = $this->get(route('company-search'));
    $response->assertRedirect(route('login'));
});

test('authenticated users can visit the company search page', function () {
    $this->actingAs(User::factory()->create());

    $this->get(route('company-search'))->assertOk();
});

test('company search page displays correctly', function () {
    $this->actingAs(User::factory()->create());

    $response = $this->get(route('company-search'));

    $response->assertSee('Unternehmensauskunft');
    $response->assertSee('Einfache Suche für Unternehmensdaten und historische Cases');
});

test('search input is wired to component', function () {
    $this->actingAs(User::factory()->create());

    Volt::test('company-search')
        ->assertSet('search', '')
        ->set('search', 'Test Firma')
        ->assertSet('search', 'Test Firma');
});

test('overview aggregates open alert metrics', function () {
    $this->actingAs(User::factory()->create());

    $acme = Company::factory()->create(['legal_name' => 'Acme Investigations AG']);
    $globex = Company::factory()->create(['legal_name' => 'Globex Risk GmbH']);

    Transaction::factory()->for($acme)->requiresReview()->status(Transaction::STATUS_TRUE_POSITIVE)->create([
        'amount' => 120000,
        'risk_score' => 90,
        'executed_at' => Carbon::parse('2024-01-10 09:00:00'),
        'signals' => ['Sanktionsliste'],
    ]);

    Transaction::factory()->for($globex)->requiresReview()->status(Transaction::STATUS_FALSE_POSITIVE)->create([
        'amount' => 45000,
        'risk_score' => 60,
        'executed_at' => Carbon::parse('2024-01-11 12:30:00'),
    ]);

    Transaction::factory()->for($acme)->requiresReview(false)->status(Transaction::STATUS_FALSE_POSITIVE)->create([
        'amount' => 8000,
        'risk_score' => 20,
        'executed_at' => Carbon::parse('2024-01-09 08:15:00'),
    ]);

    $component = Volt::test('company-search');

    $overview = $component->get('overview');

    expect($overview['companies'])->toBe(2)
        ->and($overview['open_alerts'])->toBe(2)
        ->and($overview['open_volume'])->toEqualWithDelta(165000, 0.001)
        ->and($overview['avg_risk'])->toEqualWithDelta(75, 0.001)
        ->and($overview['watchlist_hits'])->toBe(1)
        ->and($overview['automation_share'])->toBe(68);
});

test('search filters companies by ticker and name', function () {
    $this->actingAs(User::factory()->create());

    $match = Company::factory()->create([
        'name' => 'Fintrac Labs',
        'legal_name' => 'Fintrac Labs AG',
        'ticker' => 'FTR',
    ]);

    $other = Company::factory()->create([
        'name' => 'Velocity Partners',
        'ticker' => 'VLP',
    ]);

    Transaction::factory()->for($match)->requiresReview()->create(['amount' => 250000]);
    Transaction::factory()->for($other)->requiresReview()->create(['amount' => 125000]);

    $component = Volt::test('company-search');

    $component->set('search', 'FTR');

    $companies = $component->get('companies');

    expect($companies)->toHaveCount(1)
        ->and($companies->first()->id)->toBe($match->id);
});
