<?php

use App\Enums\PredictionSource;
use App\Enums\TrendDirection;
use App\Models\BrentPrice;
use App\Models\PricePrediction;
use App\Services\BrentPricePredictor;
use App\Services\LlmPrediction\OilPredictionProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    $this->provider = Mockery::mock(OilPredictionProvider::class);
    $this->predictor = new BrentPricePredictor($this->provider);
});

it('detects a rising trend when 3-day EWMA exceeds 7-day EWMA by threshold', function (): void {
    $prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
        'date' => now()->subDays(14 - $i)->toDateString(),
        'price_usd' => 70.0 + ($i * 2.0),
    ]));

    $prediction = $this->predictor->generateEwmaPrediction($prices);

    expect($prediction->direction)->toBe(TrendDirection::Rising)
        ->and($prediction->source)->toBe(PredictionSource::Ewma)
        ->and($prediction->confidence)->toBeGreaterThan(0)
        ->and($prediction->confidence)->toBeLessThanOrEqual(65);
});

it('detects a falling trend when 3-day EWMA falls below 7-day EWMA by threshold', function (): void {
    $prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
        'date' => now()->subDays(14 - $i)->toDateString(),
        'price_usd' => 85.0 - ($i * 2.0),
    ]));

    $prediction = $this->predictor->generateEwmaPrediction($prices);

    expect($prediction->direction)->toBe(TrendDirection::Falling);
});

it('returns flat when price movement is within threshold', function (): void {
    $prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
        'date' => now()->subDays(14 - $i)->toDateString(),
        'price_usd' => 75.0 + (($i % 2 === 0) ? 0.1 : -0.1),
    ]));

    $prediction = $this->predictor->generateEwmaPrediction($prices);

    expect($prediction->direction)->toBe(TrendDirection::Flat)
        ->and($prediction->confidence)->toBe(50);
});

it('returns null when fewer than 14 prices are available for EWMA', function (): void {
    $prices = collect(range(1, 10))->map(fn (int $i) => new BrentPrice([
        'date' => now()->subDays(10 - $i)->toDateString(),
        'price_usd' => 75.0,
    ]));

    expect($this->predictor->generateEwmaPrediction($prices))->toBeNull();
});

it('stores both EWMA and LLM predictions when provider succeeds', function (): void {
    seedPrices(20);

    $this->provider->shouldReceive('predict')->once()->andReturn(new PricePrediction([
        'predicted_for' => now()->toDateString(),
        'source' => PredictionSource::LlmWithContext,
        'direction' => TrendDirection::Rising,
        'confidence' => 70,
        'reasoning' => 'Trend is up.',
        'generated_at' => now(),
    ]));

    $prediction = $this->predictor->generatePrediction();

    expect($prediction->source)->toBe(PredictionSource::LlmWithContext)
        ->and(PricePrediction::count())->toBe(2);
});

it('falls back to EWMA when provider returns null', function (): void {
    seedPrices(20, slope: 0.8);

    $this->provider->shouldReceive('predict')->once()->andReturn(null);

    $prediction = $this->predictor->generatePrediction();

    expect($prediction->source)->toBe(PredictionSource::Ewma)
        ->and(PricePrediction::count())->toBe(1);
});

it('returns null when there is insufficient price data', function (): void {
    BrentPrice::insert([
        ['date' => now()->subDays(2)->toDateString(), 'price_usd' => 75.0],
        ['date' => now()->subDay()->toDateString(), 'price_usd' => 76.0],
    ]);

    $this->provider->shouldNotReceive('predict');

    expect($this->predictor->generatePrediction())->toBeNull()
        ->and(PricePrediction::count())->toBe(0);
});

it('flags latest brent price as prediction generated on success', function (): void {
    seedPrices(20);

    $this->provider->shouldReceive('predict')->once()->andReturn(null);

    $this->predictor->generatePrediction();

    $latest = BrentPrice::orderBy('date', 'desc')->first();

    expect($latest->prediction_generated_at)->not->toBeNull();
});

it('does not flag when prediction cannot be generated', function (): void {
    BrentPrice::insert([
        ['date' => now()->subDay()->toDateString(), 'price_usd' => 75.0],
    ]);

    $this->provider->shouldNotReceive('predict');

    $this->predictor->generatePrediction();

    expect(BrentPrice::first()->prediction_generated_at)->toBeNull();
});

it('returns the latest price row', function (): void {
    seedPrices(3);

    expect($this->predictor->latestPrice())->not->toBeNull()
        ->and($this->predictor->latestPrice()->date->toDateString())->toBe(now()->toDateString());
});

function seedPrices(int $count, float $slope = 1.0): void
{
    BrentPrice::insert(
        collect(range(1, $count))->map(fn (int $i) => [
            'date' => now()->subDays($count - $i)->toDateString(),
            'price_usd' => 75.0 + ($i * $slope),
        ])->all()
    );
}
