<?php

use Illuminate\Database\QueryException;
use Modules\Routing\Models\EvRoute;
use Modules\Routing\Models\RoutePricing;
use Modules\Shared\Enums\VehicleOption;

test('route pricing belongs to a route and casts its vehicle option, price and blocked flag', function () {
    $route = EvRoute::factory()->create();

    $pricing = RoutePricing::factory()->create([
        'ev_route_id' => $route->id,
        'vehicle_option' => VehicleOption::FrontSeat,
        'price' => 12000,
        'is_blocked' => true,
    ]);

    expect($pricing->route)->toBeInstanceOf(EvRoute::class)
        ->and($pricing->route->is($route))->toBeTrue()
        ->and($pricing->vehicle_option)->toBe(VehicleOption::FrontSeat)
        ->and($pricing->price)->toEqual('12000.00')
        ->and($pricing->is_blocked)->toBeTrue();

    expect($route->pricing()->first()->is($pricing))->toBeTrue();
});

test('a route can only have one price per vehicle option', function () {
    $route = EvRoute::factory()->create();

    RoutePricing::factory()->create([
        'ev_route_id' => $route->id,
        'vehicle_option' => VehicleOption::WholeVehicle,
    ]);

    expect(fn () => RoutePricing::factory()->create([
        'ev_route_id' => $route->id,
        'vehicle_option' => VehicleOption::WholeVehicle,
    ]))->toThrow(QueryException::class);
});

test('a route can have all three vehicle options priced', function () {
    $route = EvRoute::factory()->create();

    foreach (VehicleOption::cases() as $option) {
        RoutePricing::factory()->create([
            'ev_route_id' => $route->id,
            'vehicle_option' => $option,
        ]);
    }

    expect($route->pricing()->count())->toBe(3);
});

test('pricing defaults to unblocked', function () {
    $pricing = RoutePricing::factory()->create();

    expect($pricing->is_blocked)->toBeFalse();
});
