<?php

namespace Tests\Feature;

use App\Models\Room;
use App\Models\RoomBooking;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class RoomBookingFlowTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_can_create_room_booking(): void
    {
        $user = User::factory()->create(['role' => 'user']);
        $room = Room::create([
            'name' => 'Ruang Rapat 1',
            'building' => 'Gedung A',
            'floor' => '1',
            'capacity' => 20,
            'type' => 'meeting',
            'manual_status' => 'available',
        ]);

        $response = $this->actingAs($user)->post(route('rooms.booking.store'), [
            'room_id' => $room->id,
            'title' => 'Rapat Koordinasi',
            'start_time' => now()->addHour()->format('Y-m-d H:i:s'),
            'end_time' => now()->addHours(2)->format('Y-m-d H:i:s'),
        ]);

        $response->assertSessionHas('success');
        $this->assertDatabaseHas('room_bookings', [
            'user_id' => $user->id,
            'room_id' => $room->id,
            'title' => 'Rapat Koordinasi',
            'status' => 'pending',
        ]);
    }

    public function test_room_booking_is_rejected_when_schedule_conflicts(): void
    {
        $user = User::factory()->create(['role' => 'user']);
        $room = Room::create([
            'name' => 'Ruang Rapat 2',
            'building' => 'Gedung B',
            'floor' => '2',
            'capacity' => 10,
            'type' => 'meeting',
            'manual_status' => 'available',
        ]);
        RoomBooking::create([
            'user_id' => $user->id,
            'room_id' => $room->id,
            'title' => 'Booking Lama',
            'start_time' => now()->addHours(3),
            'end_time' => now()->addHours(5),
            'status' => 'approved',
        ]);

        $response = $this->from(route('rooms.index'))->actingAs($user)->post(route('rooms.booking.store'), [
            'room_id' => $room->id,
            'title' => 'Booking Bentrok',
            'start_time' => now()->addHours(4)->format('Y-m-d H:i:s'),
            'end_time' => now()->addHours(6)->format('Y-m-d H:i:s'),
        ]);

        $response->assertRedirect(route('rooms.index'));
        $response->assertSessionHas('error');
        $this->assertDatabaseCount('room_bookings', 1);
    }

    public function test_kepala_bidang_can_approve_room_booking(): void
    {
        $user = User::factory()->create(['role' => 'user']);
        $kepala = User::factory()->create(['role' => 'kepala_bidang']);
        $room = Room::create([
            'name' => 'Ruang Rapat 3',
            'building' => 'Gedung C',
            'floor' => '3',
            'capacity' => 30,
            'type' => 'meeting',
            'manual_status' => 'available',
        ]);
        $booking = RoomBooking::create([
            'user_id' => $user->id,
            'room_id' => $room->id,
            'title' => 'Forum Diskusi',
            'start_time' => now()->addHours(3),
            'end_time' => now()->addHours(4),
            'status' => 'pending',
        ]);

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

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