<?php

use App\Models\Station;
use App\Models\StationPrice;
use App\Models\StationPriceArchive;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('moves prices older than 12 months to archive', function (): void {
    $station = Station::factory()->create();

    StationPrice::factory()->create([
        'station_id' => $station->node_id,
        'price_effective_at' => now()->subMonths(13),
        'price_reported_at' => now()->subMonths(13),
        'recorded_at' => now()->subMonths(13),
    ]);

    StationPrice::factory()->create([
        'station_id' => $station->node_id,
        'price_effective_at' => now()->subMonths(6),
        'price_reported_at' => now()->subMonths(6),
        'recorded_at' => now()->subMonths(6),
    ]);

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

    expect(StationPrice::count())->toBe(1)
        ->and(StationPriceArchive::count())->toBe(1);
});

it('outputs no-op message when nothing qualifies', function (): void {
    $station = Station::factory()->create();

    StationPrice::factory()->create([
        'station_id' => $station->node_id,
        'price_effective_at' => now()->subMonths(3),
        'price_reported_at' => now()->subMonths(3),
        'recorded_at' => now()->subMonths(3),
    ]);

    $this->artisan('fuel:archive')
        ->expectsOutputToContain('No prices to archive.')
        ->assertSuccessful();

    expect(StationPrice::count())->toBe(1)
        ->and(StationPriceArchive::count())->toBe(0);
});

it('preserves the row data when archiving', function (): void {
    $station = Station::factory()->create();

    $original = StationPrice::factory()->create([
        'station_id' => $station->node_id,
        'price_pence' => 14523,
        'price_effective_at' => now()->subMonths(13),
        'price_reported_at' => now()->subMonths(13),
        'recorded_at' => now()->subMonths(13),
    ]);

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

    $archived = StationPriceArchive::first();

    expect($archived)->not->toBeNull()
        ->and($archived->station_id)->toBe($original->station_id)
        ->and($archived->price_pence)->toBe(14523);
});
