<?php

namespace Tests\Feature;

use App\Models\Divisi;
use App\Models\User;
use App\Models\Vehicle;
use App\Models\VehicleBooking;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class VehicleBookingFlowTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_can_create_vehicle_booking(): void
    {
        $divisi = Divisi::create(['nama' => 'Operasional']);
        $user = User::factory()->create(['role' => 'user', 'divisi_id' => $divisi->id]);
        $vehicle = Vehicle::create([
            'name' => 'Toyota Avanza',
            'type' => 'mobil',
            'plate_number' => 'L 1234 AB',
            'brand' => 'Toyota',
            'model' => 'Avanza',
            'year' => 2022,
            'color' => 'Hitam',
            'fuel_type' => 'Bensin',
            'capacity' => 7,
            'status' => 'available',
            'divisi_id' => $divisi->id,
        ]);

        $response = $this->actingAs($user)->post(route('vehicles.booking.store'), [
            'vehicle_id' => $vehicle->id,
            'purpose' => 'Kunjungan lapangan',
            'destination' => 'Surabaya Barat',
            'start_time' => now()->addHour()->format('Y-m-d H:i:s'),
            'end_time' => now()->addHours(3)->format('Y-m-d H:i:s'),
            'notes' => 'Berangkat pagi',
        ]);

        $response->assertSessionHas('success');
        $this->assertDatabaseHas('vehicle_bookings', [
            'user_id' => $user->id,
            'vehicle_id' => $vehicle->id,
            'purpose' => 'Kunjungan lapangan',
            'destination' => 'Surabaya Barat',
            'status' => 'pending',
        ]);
    }

    public function test_vehicle_booking_is_rejected_when_schedule_conflicts(): void
    {
        $divisi = Divisi::create(['nama' => 'Transport']);
        $user = User::factory()->create(['role' => 'user', 'divisi_id' => $divisi->id]);
        $vehicle = Vehicle::create([
            'name' => 'Honda Beat',
            'type' => 'motor',
            'plate_number' => 'L 9999 CD',
            'brand' => 'Honda',
            'model' => 'Beat',
            'year' => 2023,
            'color' => 'Putih',
            'fuel_type' => 'Bensin',
            'capacity' => 2,
            'status' => 'available',
            'divisi_id' => $divisi->id,
        ]);
        VehicleBooking::create([
            'vehicle_id' => $vehicle->id,
            'user_id' => $user->id,
            'purpose' => 'Booking lama',
            'destination' => 'Kantor',
            'start_time' => now()->addHours(2),
            'end_time' => now()->addHours(5),
            'status' => 'approved',
        ]);

        $response = $this->actingAs($user)->post(route('vehicles.booking.store'), [
            'vehicle_id' => $vehicle->id,
            'purpose' => 'Booking bentrok',
            'destination' => 'Lokasi lain',
            'start_time' => now()->addHours(3)->format('Y-m-d H:i:s'),
            'end_time' => now()->addHours(4)->format('Y-m-d H:i:s'),
        ]);

        $response->assertSessionHas('error');
        $this->assertDatabaseCount('vehicle_bookings', 1);
    }

    public function test_kepala_bidang_can_approve_vehicle_booking(): void
    {
        $divisi = Divisi::create(['nama' => 'Mobilitas']);
        $user = User::factory()->create(['role' => 'user', 'divisi_id' => $divisi->id]);
        $kepala = User::factory()->create(['role' => 'kepala_bidang', 'divisi_id' => $divisi->id]);
        $vehicle = Vehicle::create([
            'name' => 'Suzuki Ertiga',
            'type' => 'mobil',
            'plate_number' => 'L 7777 EF',
            'brand' => 'Suzuki',
            'model' => 'Ertiga',
            'year' => 2021,
            'color' => 'Silver',
            'fuel_type' => 'Bensin',
            'capacity' => 7,
            'status' => 'available',
            'divisi_id' => $divisi->id,
        ]);
        $booking = VehicleBooking::create([
            'vehicle_id' => $vehicle->id,
            'user_id' => $user->id,
            'purpose' => 'Dinas luar',
            'destination' => 'Sidoarjo',
            'start_time' => now()->addHours(5),
            'end_time' => now()->addHours(7),
            'status' => 'pending',
        ]);

        $response = $this->actingAs($kepala)->post(route('verification.vehicle.action', $booking->id), [
            'action' => 'approve',
        ]);

        $response->assertSessionHas('success');
        $this->assertSame('approved', $booking->fresh()->status);
    }
}
