<?php

use App\Models\Station;
use App\Models\StationPrice;
use App\Models\StationPriceCurrent;
use App\Services\ApiLogger;
use App\Services\FuelPriceService;
use App\Services\StationTaggingService;
use Carbon\CarbonInterface;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    $this->service = new FuelPriceService(new StationTaggingService, new ApiLogger);
});

it('fetches and caches an access token', function (): void {
    Http::fake([
        '*/oauth/generate_access_token' => Http::response([
            'data' => [
                'access_token' => 'test-token-abc',
                'expires_in' => 3600,
            ],
        ]),
    ]);

    $token = $this->service->getAccessToken();

    expect($token)->toBe('test-token-abc')
        ->and(Cache::get('fuel_finder_access_token'))->toBe('test-token-abc');
});

it('returns cached token without hitting API', function (): void {
    Cache::put('fuel_finder_access_token', 'cached-token', 3540);

    Http::fake();

    $token = $this->service->getAccessToken();

    expect($token)->toBe('cached-token');
    Http::assertNothingSent();
});

it('upserts stations from API batch response', function (): void {
    $apiStations = [
        [
            'node_id' => 'abc123',
            'trading_name' => 'Village Garage',
            'brand_name' => 'Village Garage',
            'is_same_trading_and_brand_name' => true,
            'is_motorway_service_station' => false,
            'is_supermarket_service_station' => false,
            'temporary_closure' => false,
            'permanent_closure' => false,
            'permanent_closure_date' => null,
            'public_phone_number' => null,
            'location' => [
                'address_line_1' => '1 High Street',
                'address_line_2' => null,
                'city' => 'London',
                'county' => null,
                'country' => 'England',
                'postcode' => 'SW1A 1AA',
                'latitude' => 51.5,
                'longitude' => -0.1,
            ],
            'amenities' => [],
            'opening_times' => null,
            'fuel_types' => ['E10', 'E5'],
        ],
    ];

    $this->service->upsertStations($apiStations);

    $station = Station::find('abc123');
    expect($station)->not->toBeNull()
        ->and($station->trading_name)->toBe('Village Garage')
        ->and($station->postcode)->toBe('SW1A 1AA')
        ->and((float) $station->lat)->toBe(51.5)
        ->and($station->is_supermarket)->toBeFalse();
});

it('tags supermarket stations during upsert', function (): void {
    $apiStations = [[
        'node_id' => 'tesco1',
        'trading_name' => 'TESCO',
        'brand_name' => 'TESCO',
        'is_same_trading_and_brand_name' => true,
        'is_motorway_service_station' => false,
        'is_supermarket_service_station' => true,
        'temporary_closure' => false,
        'permanent_closure' => false,
        'permanent_closure_date' => null,
        'public_phone_number' => null,
        'location' => [
            'address_line_1' => '1 Tesco Way',
            'address_line_2' => null,
            'city' => 'Bristol',
            'county' => null,
            'country' => 'England',
            'postcode' => 'BS1 1AA',
            'latitude' => 51.45,
            'longitude' => -2.6,
        ],
        'amenities' => [],
        'opening_times' => null,
        'fuel_types' => ['E10'],
    ]];

    $this->service->upsertStations($apiStations);

    expect(Station::find('tesco1')->is_supermarket)->toBeTrue();
});

// --- pollPrices ---

it('inserts new price records and upserts current prices on poll', function (): void {
    Cache::put('fuel_finder_access_token', 'tok', 3540);

    Http::fake([
        '*/pfs/fuel-prices*' => Http::sequence()
            ->push([
                [
                    'node_id' => 'sta1',
                    'fuel_prices' => [
                        [
                            'fuel_type' => 'E10',
                            'price' => 142.9,
                            'price_last_updated' => '2026-04-04T10:00:00.000Z',
                            'price_change_effective_timestamp' => '2026-04-04T10:00:00.000Z',
                        ],
                    ],
                ],
            ])
            ->push([]),
    ]);

    Station::factory()->create(['node_id' => 'sta1']);

    $inserted = $this->service->pollPrices();

    expect($inserted)->toBe(1)
        ->and(StationPrice::count())->toBe(1)
        ->and(StationPriceCurrent::where('station_id', 'sta1')->where('fuel_type', 'e10')->value('price_pence'))->toBe(14290);
});

it('does not insert a history row when price is unchanged', function (): void {
    Cache::put('fuel_finder_access_token', 'tok', 3540);

    Station::factory()->create(['node_id' => 'sta1']);

    StationPriceCurrent::insert([
        'station_id' => 'sta1',
        'fuel_type' => 'e10',
        'price_pence' => 14290,
        'price_effective_at' => now()->subHour(),
        'price_reported_at' => now()->subHour(),
        'recorded_at' => now()->subHour(),
    ]);

    Http::fake([
        '*/pfs/fuel-prices*' => Http::sequence()
            ->push([
                [
                    'node_id' => 'sta1',
                    'fuel_prices' => [
                        [
                            'fuel_type' => 'E10',
                            'price' => 142.9,
                            'price_last_updated' => '2026-04-04T10:00:00.000Z',
                            'price_change_effective_timestamp' => '2026-04-04T10:00:00.000Z',
                        ],
                    ],
                ],
            ])
            ->push([]),
    ]);

    $inserted = $this->service->pollPrices();

    expect($inserted)->toBe(0)
        ->and(StationPrice::count())->toBe(0);
});

it('inserts a history row when price has changed', function (): void {
    Cache::put('fuel_finder_access_token', 'tok', 3540);

    Station::factory()->create(['node_id' => 'sta1']);

    StationPriceCurrent::insert([
        'station_id' => 'sta1',
        'fuel_type' => 'e10',
        'price_pence' => 14290,
        'price_effective_at' => now()->subHour(),
        'price_reported_at' => now()->subHour(),
        'recorded_at' => now()->subHour(),
    ]);

    Http::fake([
        '*/pfs/fuel-prices*' => Http::sequence()
            ->push([
                [
                    'node_id' => 'sta1',
                    'fuel_prices' => [
                        [
                            'fuel_type' => 'E10',
                            'price' => 139.9,
                            'price_last_updated' => '2026-04-04T12:00:00.000Z',
                            'price_change_effective_timestamp' => '2026-04-04T12:00:00.000Z',
                        ],
                    ],
                ],
            ])
            ->push([]),
    ]);

    $inserted = $this->service->pollPrices();

    expect($inserted)->toBe(1)
        ->and(StationPrice::first()->price_pence)->toBe(13990)
        ->and(StationPriceCurrent::where('station_id', 'sta1')->value('price_pence'))->toBe(13990);
});

it('skips unknown fuel types without error', function (): void {
    Cache::put('fuel_finder_access_token', 'tok', 3540);

    Station::factory()->create(['node_id' => 'sta1']);

    Http::fake([
        '*/pfs/fuel-prices*' => Http::sequence()
            ->push([
                [
                    'node_id' => 'sta1',
                    'fuel_prices' => [
                        [
                            'fuel_type' => 'UNKNOWN_FUEL',
                            'price' => 150.0,
                            'price_last_updated' => '2026-04-04T10:00:00.000Z',
                            'price_change_effective_timestamp' => '2026-04-04T10:00:00.000Z',
                        ],
                    ],
                ],
            ])
            ->push([]),
    ]);

    $inserted = $this->service->pollPrices();

    expect($inserted)->toBe(0)
        ->and(StationPrice::count())->toBe(0);
});

it('skips prices outside valid range and logs a warning', function (): void {
    Cache::put('fuel_finder_access_token', 'tok', 3540);

    Station::factory()->create(['node_id' => 'sta1']);

    Http::fake([
        '*/pfs/fuel-prices*' => Http::sequence()
            ->push([
                [
                    'node_id' => 'sta1',
                    'fuel_prices' => [
                        // Way too high — clearly bad data
                        [
                            'fuel_type' => 'E10',
                            'price' => 900.0,
                            'price_last_updated' => '2026-04-04T10:00:00.000Z',
                            'price_change_effective_timestamp' => '2026-04-04T10:00:00.000Z',
                        ],
                        // Too low — below minimum
                        [
                            'fuel_type' => 'E5',
                            'price' => 10.0,
                            'price_last_updated' => '2026-04-04T10:00:00.000Z',
                            'price_change_effective_timestamp' => '2026-04-04T10:00:00.000Z',
                        ],
                        // Valid — should be inserted
                        [
                            'fuel_type' => 'B7_STANDARD',
                            'price' => 155.9,
                            'price_last_updated' => '2026-04-04T10:00:00.000Z',
                            'price_change_effective_timestamp' => '2026-04-04T10:00:00.000Z',
                        ],
                    ],
                ],
            ])
            ->push([]),
    ]);

    $inserted = $this->service->pollPrices();

    expect($inserted)->toBe(1)
        ->and(StationPrice::count())->toBe(1)
        ->and(StationPrice::first()->price_pence)->toBe(15590);
});

it('stops pagination when an empty batch is returned', function (): void {
    Cache::put('fuel_finder_access_token', 'tok', 3540);

    Http::fake([
        '*/pfs/fuel-prices*' => Http::sequence()
            ->push([])
            ->push([['node_id' => 'never', 'fuel_prices' => []]]),
    ]);

    $this->service->pollPrices();

    Http::assertSentCount(1);
});

it('caches the poll timestamp and sends it on subsequent polls', function (): void {
    Cache::put('fuel_finder_access_token', 'tok', 3540);
    Cache::forget('fuel_finder_last_price_poll_at');

    Http::fake([
        '*/pfs/fuel-prices*' => Http::response([]),
    ]);

    $this->service->pollPrices();

    expect(Cache::get('fuel_finder_last_price_poll_at'))->toBeInstanceOf(CarbonInterface::class);

    $this->service->pollPrices();

    Http::assertSent(fn ($request) => str_contains($request->url(), 'effective-start-timestamp='));
});

it('does not cache the poll timestamp when a batch errors', function (): void {
    Cache::put('fuel_finder_access_token', 'tok', 3540);
    Cache::forget('fuel_finder_last_price_poll_at');

    Http::fake([
        '*/pfs/fuel-prices*' => Http::response([], 500),
    ]);

    $this->service->pollPrices();

    expect(Cache::has('fuel_finder_last_price_poll_at'))->toBeFalse();
});

it('skips price rows for stations not present in the stations table', function (): void {
    Cache::put('fuel_finder_access_token', 'tok', 3540);

    Http::fake([
        '*/pfs/fuel-prices*' => Http::sequence()
            ->push([[
                'node_id' => 'unknown-station',
                'fuel_prices' => [[
                    'fuel_type' => 'E10',
                    'price' => 142.9,
                    'price_last_updated' => '2026-04-04T10:00:00.000Z',
                    'price_change_effective_timestamp' => '2026-04-04T10:00:00.000Z',
                ]],
            ]])
            ->push([]),
    ]);

    $inserted = $this->service->pollPrices();

    expect($inserted)->toBe(0)
        ->and(StationPrice::count())->toBe(0)
        ->and(StationPriceCurrent::count())->toBe(0);
});

it('normalises amenities and fuel_types object payloads to flat arrays', function (): void {
    $apiStations = [[
        'node_id' => 'abc999',
        'trading_name' => 'Shell Somewhere',
        'brand_name' => 'Shell',
        'is_same_trading_and_brand_name' => false,
        'is_motorway_service_station' => false,
        'is_supermarket_service_station' => false,
        'temporary_closure' => false,
        'permanent_closure' => false,
        'location' => [
            'postcode' => 'AB1 2CD',
            'latitude' => 52.1,
            'longitude' => -1.2,
        ],
        'amenities' => [
            'adblue_pumps' => true,
            'car_wash' => false,
            'customer_toilets' => true,
        ],
        'fuel_types' => [
            'E10' => true,
            'E5' => true,
            'B7_Standard' => true,
            'B7_Premium' => false,
            'B10' => false,
            'HVO' => false,
        ],
    ]];

    $this->service->upsertStations($apiStations);

    $station = Station::find('abc999');
    expect($station->amenities)->toBe(['adblue_pumps', 'customer_toilets'])
        ->and($station->fuel_types)->toBe(['E10', 'E5', 'B7_Standard']);
});

it('skips stations missing required fields', function (): void {
    $apiStations = [
        ['node_id' => 'missing-loc', 'trading_name' => 'Bad Data'],
        [
            'node_id' => 'good',
            'trading_name' => 'Good Station',
            'location' => ['postcode' => 'AB1 2CD', 'latitude' => 52.0, 'longitude' => -1.0],
        ],
    ];

    $this->service->upsertStations($apiStations);

    expect(Station::find('missing-loc'))->toBeNull()
        ->and(Station::find('good'))->not->toBeNull();
});
