<?php

use App\Enums\FuelType;
use App\Models\Station;
use App\Models\StationPriceCurrent;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

beforeEach(function () {
    $this->withHeaders(['X-Api-Key' => config('app.api_secret_key')]);
});

it('returns a prediction response for diesel', function () {
    $this->getJson('/api/prediction?fuel_type=diesel')
        ->assertOk()
        ->assertJsonStructure([
            'fuel_type', 'current_avg', 'predicted_direction', 'predicted_change_pence',
            'confidence_score', 'confidence_label', 'action', 'reasoning',
            'prediction_horizon_days', 'region_key', 'methodology',
            'signals' => [
                'trend' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
                'day_of_week' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
                'brand_behaviour' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
                'national_momentum' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
                'regional_momentum' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
                'price_stickiness' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
            ],
        ])
        ->assertJsonPath('fuel_type', 'b7_standard')
        ->assertJsonPath('region_key', 'national');
});

it('includes current average from live prices', function () {
    $station = Station::factory()->create();
    StationPriceCurrent::factory()->create([
        'station_id' => $station->node_id,
        'fuel_type' => FuelType::B7Standard,
        'price_pence' => 14750,
    ]);

    $response = $this->getJson('/api/prediction?fuel_type=diesel')->assertOk();

    expect($response->json('current_avg'))->toBe(147.5);
});

it('accepts optional lat and lng for regional context', function () {
    $this->getJson('/api/prediction?fuel_type=diesel&lat=52.5&lng=-0.2')
        ->assertOk()
        ->assertJsonPath('region_key', 'national'); // still national, regional_momentum signal updated internally
});

it('returns 422 when fuel_type is missing', function () {
    $this->getJson('/api/prediction')
        ->assertUnprocessable()
        ->assertJsonValidationErrors(['fuel_type']);
});

it('returns 422 for unknown fuel_type alias', function () {
    $this->getJson('/api/prediction?fuel_type=rocket_fuel')
        ->assertUnprocessable()
        ->assertJsonValidationErrors(['fuel_type']);
});
