<?php

use App\Models\User;
use Illuminate\Support\Facades\Cache;
use Modules\Routing\Models\EvRoute;
use Modules\Routing\Models\RoutePricing;
use Modules\Shared\Enums\VehicleOption;

beforeEach(function () {
    $this->token = User::factory()->create()->createToken('test-token')->plainTextToken;
});

test('the pricing endpoint serves a cached response until invalidated', function () {
    $route = EvRoute::factory()->create(['is_active' => true]);
    $pricing = RoutePricing::factory()->create([
        'ev_route_id' => $route->id,
        'vehicle_option' => VehicleOption::FrontSeat,
        'price' => 10000,
    ]);

    $this->withHeader('Authorization', "Bearer {$this->token}")
        ->getJson("/api/v1/routes/{$route->id}/pricing")
        ->assertJsonFragment(['price' => '10000.00']);

    // Bypass Eloquent (no 'saved' event) so a stale cached response proves caching is active.
    RoutePricing::query()->where('id', $pricing->id)->update(['price' => 99999]);

    $this->withHeader('Authorization', "Bearer {$this->token}")
        ->getJson("/api/v1/routes/{$route->id}/pricing")
        ->assertJsonFragment(['price' => '10000.00']);

    // A genuine Eloquent save fires the observer and flushes the 'routes' tag.
    $pricing->refresh()->save();

    $this->withHeader('Authorization', "Bearer {$this->token}")
        ->getJson("/api/v1/routes/{$route->id}/pricing")
        ->assertJsonFragment(['price' => '99999.00']);
});

test('saving an ev route invalidates the routes cache tag', function () {
    $route = EvRoute::factory()->create(['is_active' => true]);

    $this->withHeader('Authorization', "Bearer {$this->token}")
        ->postJson('/api/v1/routes/search')
        ->assertJsonCount(1, 'routes.data');

    // Bypass Eloquent so the change wouldn't be visible without invalidation.
    EvRoute::query()->where('id', $route->id)->update(['is_active' => false]);

    $this->withHeader('Authorization', "Bearer {$this->token}")
        ->postJson('/api/v1/routes/search')
        ->assertJsonCount(1, 'routes.data');

    $route->refresh()->save();

    $this->withHeader('Authorization', "Bearer {$this->token}")
        ->postJson('/api/v1/routes/search')
        ->assertJsonCount(0, 'routes.data');
});

test('deleting an ev route invalidates the routes cache tag', function () {
    $route = EvRoute::factory()->create(['is_active' => true]);

    $this->withHeader('Authorization', "Bearer {$this->token}")
        ->postJson('/api/v1/routes/search')
        ->assertJsonCount(1, 'routes.data');

    $route->delete();

    $this->withHeader('Authorization', "Bearer {$this->token}")
        ->postJson('/api/v1/routes/search')
        ->assertJsonCount(0, 'routes.data');
});

test('flushing the routes cache tag does not affect other cached data', function () {
    Cache::put('unrelated-key', 'still here', now()->addMinutes(5));

    $route = EvRoute::factory()->create(['is_active' => true]);
    $route->update(['is_active' => false]);

    expect(Cache::get('unrelated-key'))->toBe('still here');
});
