<?php

use App\Models\Station;
use App\Models\StationPriceCurrent;
use App\Services\Forecasting\LocalSnapshotService;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

function seedStation(float $lat, float $lng, int $pence, bool $supermarket = false, ?string $name = 'Test', ?string $brand = null): Station
{
    $s = Station::factory()->create([
        'lat' => $lat,
        'lng' => $lng,
        'is_supermarket' => $supermarket,
        'trading_name' => $name,
        'brand_name' => $brand,
    ]);
    StationPriceCurrent::factory()->create([
        'station_id' => $s->node_id,
        'fuel_type' => 'e10',
        'price_pence' => $pence,
    ]);

    return $s;
}

it('returns the national average across all stations regardless of geo', function () {
    seedStation(51.5, -0.1, 14000);
    seedStation(53.5, -2.2, 15000);

    $snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);

    expect($snapshot['national_avg_pence'])->toBe(145.0);
});

it('returns the local average filtered to within 50km', function () {
    seedStation(51.5, -0.1, 14000);    // London → near coord
    seedStation(53.5, -2.2, 16000);    // Manchester → far

    $snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);

    expect($snapshot['local_avg_pence'])->toBe(140.0)
        ->and($snapshot['local_minus_national_pence'])->toBe(-10.0);
});

it('returns the cheapest nearby stations sorted by price ascending', function () {
    seedStation(51.5010, -0.1415, 14500, name: 'A');
    seedStation(51.5020, -0.1420, 14000, name: 'B');
    seedStation(51.5030, -0.1430, 14250, name: 'C');

    $snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.14);

    expect($snapshot['cheapest_nearby'])->toHaveCount(3)
        ->and($snapshot['cheapest_nearby'][0]['price_pence'])->toBe(14000)
        ->and($snapshot['cheapest_nearby'][0]['name'])->toBe('B')
        ->and($snapshot['cheapest_nearby'][2]['price_pence'])->toBe(14500);
});

it('caps cheapest_nearby at 5 even when more match', function () {
    for ($i = 0; $i < 8; $i++) {
        seedStation(51.5 + $i * 0.001, -0.1, 14000 + $i * 50);
    }

    $snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);

    expect($snapshot['cheapest_nearby'])->toHaveCount(5);
});

it('computes the supermarket / major split and the gap', function () {
    seedStation(51.5, -0.1, 14000, supermarket: true, name: 'Asda');
    seedStation(51.501, -0.101, 14200, supermarket: true, name: 'Tesco');
    seedStation(51.502, -0.102, 14600, supermarket: false, name: 'Shell');
    seedStation(51.503, -0.103, 14800, supermarket: false, name: 'BP');

    $snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);

    // Supermarket avg = 141, major avg = 147, gap = -6.0
    expect($snapshot['supermarket_avg_pence'])->toBe(141.0)
        ->and($snapshot['major_avg_pence'])->toBe(147.0)
        ->and($snapshot['supermarket_gap_pence'])->toBe(-6.0);
});

it('returns null gap when one side is empty', function () {
    seedStation(51.5, -0.1, 14000, supermarket: true);

    $snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);

    expect($snapshot['supermarket_avg_pence'])->toBe(140.0)
        ->and($snapshot['major_avg_pence'])->toBeNull()
        ->and($snapshot['supermarket_gap_pence'])->toBeNull();
});

it('counts stations within radius', function () {
    seedStation(51.5, -0.1, 14000);
    seedStation(51.501, -0.101, 14200);
    seedStation(53.5, -2.2, 14400);     // far away

    $snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1, 25);

    expect($snapshot['stations_within_radius'])->toBe(2);
});

it('returns null prices when there is no data at all', function () {
    $snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);

    expect($snapshot['national_avg_pence'])->toBeNull()
        ->and($snapshot['local_avg_pence'])->toBeNull()
        ->and($snapshot['supermarket_avg_pence'])->toBeNull()
        ->and($snapshot['major_avg_pence'])->toBeNull()
        ->and($snapshot['cheapest_nearby'])->toBe([])
        ->and($snapshot['stations_within_radius'])->toBe(0);
});
