<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 1. Tabel Master Ruangan
        Schema::create('rooms', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('building'); // Sesuai form: wajib diisi
            $table->string('floor');    // Sesuai form: wajib diisi
            $table->integer('capacity'); // Sesuai form: wajib diisi
            $table->string('type')->default('meeting'); // meeting, office, hall, other
            
            // Status manual untuk kontrol admin (available, maintenance, closed)
            $table->string('manual_status')->default('available'); 
            
            // Kolom image untuk menyimpan path foto ruangan yang diupload di form
            $table->string('image')->nullable(); 
            
            $table->text('description')->nullable();
            $table->timestamps();
        });

        // 2. Tabel Peminjaman / Booking
        Schema::create('room_bookings', function (Blueprint $table) {
            $table->id();
            // Menggunakan onDelete cascade agar jika data ruangan dihapus, history booking ikut terhapus
            $table->foreignId('room_id')->constrained('rooms')->onDelete('cascade');
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            
            $table->string('title'); // Nama kegiatan/agenda
            $table->dateTime('start_time'); 
            $table->dateTime('end_time');   
            
            // Status booking: pending (butuh approval), approved (disetujui), rejected, cancelled
            $table->string('status')->default('pending'); 
            
            $table->text('notes')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('room_bookings');
        Schema::dropIfExists('rooms');
    }
};