<?php

use App\Models\Station;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    Cache::put('fuel_finder_access_token', 'tok', 3540);
});

it('auto-refreshes station metadata when the stations table is empty', function (): void {
    Http::fake([
        '*/pfs?*' => Http::sequence()
            ->push([[
                'node_id' => 'sta1',
                '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'],
            ]])
            ->push([]),
        '*/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([]),
    ]);

    $this->artisan('fuel:poll')->assertSuccessful();

    expect(Station::find('sta1'))->not->toBeNull();
});

it('auto-refreshes station metadata when the last refresh was yesterday', function (): void {
    Station::factory()->create([
        'node_id' => 'sta1',
        'last_seen_at' => now()->subDay()->endOfDay(),
    ]);

    Http::fake([
        '*/pfs?*' => Http::sequence()
            ->push([[
                'node_id' => 'sta1',
                'trading_name' => 'Refreshed Name',
                'brand_name' => 'Refreshed Name',
                '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'],
            ]])
            ->push([]),
        '*/pfs/fuel-prices*' => Http::sequence()
            ->push([])
            ->push([]),
    ]);

    $this->artisan('fuel:poll')->assertSuccessful();

    expect(Station::find('sta1')->trading_name)->toBe('Refreshed Name');
});

it('skips the station metadata refresh when already refreshed today', function (): void {
    Station::factory()->create([
        'node_id' => 'sta1',
        'last_seen_at' => now(),
    ]);

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

    $this->artisan('fuel:poll')->assertSuccessful();

    Http::assertNotSent(fn ($request) => str_contains($request->url(), '/pfs?'));
});
