<?php

namespace Tests\Feature\Admin;

use App\Models\Company;
use App\Models\Currency;
use App\Models\Driver;
use App\Models\Role;
use App\Models\User;
use App\Models\Vehicle;
use App\Models\VehicleType;
use Database\Seeders\PermissionSeeder;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

/**
 * A driver can own multiple vehicles (only one is "current" per shift —
 * chosen when that shift starts, see ShiftService::blockingReason()) —
 * this replaced an earlier one-vehicle-per-driver rule.
 */
class VehicleDriverAssignmentTest extends TestCase
{
    use RefreshDatabase;

    public function test_a_driver_can_be_assigned_a_second_vehicle(): void
    {
        $this->seed(PermissionSeeder::class);
        $this->seed(RoleSeeder::class);
        $company = Company::create(['name' => 'Test Co', 'slug' => 'test-co', 'code' => '501']);
        $currency = Currency::create(['name' => 'Pound Sterling', 'code' => 'GBP', 'symbol' => '£', 'decimal_places' => 2, 'is_active' => true]);
        $admin = User::factory()->create(['company_id' => $company->id, 'role_id' => Role::where('name', 'company_admin')->value('id'), 'status' => 'active']);
        $vehicleType = VehicleType::create(['company_id' => $company->id, 'currency_id' => $currency->id, 'name' => 'Saloon', 'capacity' => 4, 'base_fare' => 3, 'per_distance_rate' => 1.5]);
        $driver = Driver::create(['company_id' => $company->id, 'first_name' => 'Sam', 'last_name' => 'Ollero', 'status' => 'active']);

        Vehicle::create([
            'company_id' => $company->id, 'vehicle_type_id' => $vehicleType->id, 'driver_id' => $driver->id,
            'registration_number' => 'AB12 CDE', 'status' => 'active',
        ]);

        $response = $this->actingAs($admin)->post(route('admin.vehicles.store'), [
            'vehicle_type_id' => $vehicleType->id,
            'driver_id' => $driver->id,
            'registration_number' => 'XY99 ZZZ',
            'status' => 'active',
        ]);

        $response->assertRedirect(route('admin.vehicles.index'));
        $this->assertDatabaseHas('vehicles', ['registration_number' => 'XY99 ZZZ', 'driver_id' => $driver->id]);
        $this->assertSame(2, Vehicle::where('driver_id', $driver->id)->count());
    }

    public function test_a_vehicle_can_be_created_for_a_driver_with_no_vehicle_yet(): void
    {
        $this->seed(PermissionSeeder::class);
        $this->seed(RoleSeeder::class);
        $company = Company::create(['name' => 'Test Co', 'slug' => 'test-co', 'code' => '501']);
        $currency = Currency::create(['name' => 'Pound Sterling', 'code' => 'GBP', 'symbol' => '£', 'decimal_places' => 2, 'is_active' => true]);
        $admin = User::factory()->create(['company_id' => $company->id, 'role_id' => Role::where('name', 'company_admin')->value('id'), 'status' => 'active']);
        $vehicleType = VehicleType::create(['company_id' => $company->id, 'currency_id' => $currency->id, 'name' => 'Saloon', 'capacity' => 4, 'base_fare' => 3, 'per_distance_rate' => 1.5]);
        $driver = Driver::create(['company_id' => $company->id, 'first_name' => 'Sam', 'last_name' => 'Ollero', 'status' => 'active']);

        $response = $this->actingAs($admin)->post(route('admin.vehicles.store'), [
            'vehicle_type_id' => $vehicleType->id,
            'driver_id' => $driver->id,
            'registration_number' => 'AB12 CDE',
            'status' => 'active',
        ]);

        $response->assertRedirect(route('admin.vehicles.index'));
        $this->assertDatabaseHas('vehicles', ['registration_number' => 'AB12 CDE', 'driver_id' => $driver->id]);
    }
}
