<?php

use App\Filament\Resources\Backtests\BacktestResource;
use App\Filament\Resources\Backtests\Pages\ListBacktests;
use App\Filament\Resources\Backtests\Pages\ViewBacktest;
use App\Models\Backtest;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

beforeEach(function () {
    $this->admin = User::factory()->admin()->create();
    $this->actingAs($this->admin);
});

it('lists backtests', function () {
    $backtests = Backtest::factory()->count(3)->create();

    Livewire::test(ListBacktests::class)
        ->assertOk()
        ->assertCanSeeTableRecords($backtests);
});

it('shows the calibration table on the view page', function () {
    $backtest = Backtest::factory()->create([
        'calibration_table' => [
            '0.0-0.5' => 0.55,
            '0.5-1.0' => 0.65,
            '1.0+' => 0.72,
        ],
    ]);

    Livewire::test(ViewBacktest::class, ['record' => $backtest->id])
        ->assertOk()
        ->assertSee('0.0-0.5')
        ->assertSee('55%')
        ->assertSee('1.0+')
        ->assertSee('72%');
});

it('disables create and edit', function () {
    $backtest = Backtest::factory()->create();

    expect(BacktestResource::canCreate())->toBeFalse()
        ->and(BacktestResource::canEdit($backtest))->toBeFalse()
        ->and(BacktestResource::canDelete($backtest))->toBeFalse();
});

it('filters backtests below the ship gate', function () {
    $shippable = Backtest::factory()->create(['directional_accuracy' => 65.00]);
    $marginal = Backtest::factory()->create(['directional_accuracy' => 58.00]);

    Livewire::test(ListBacktests::class)
        ->filterTable('below_ship_gate')
        ->assertCanSeeTableRecords([$marginal])
        ->assertCanNotSeeTableRecords([$shippable]);
});
