<?php

namespace App\Observers;

use Illuminate\Support\Facades\Auth;

use App\Models\Room;
use App\Models\RoomStatusLog;

class RoomObserver
{
    /**
     * Handle the Room "created" event.
     */
    public function created(Room $room): void
    {
        //
    }

    /**
     * Handle the Room "updated" event.
     */
    public function updated(Room $room): void
    {
        // 檢查是否有變更狀態
        if ($room->wasChanged('status')) {
            RoomStatusLog::create([
                'room_id' => $room->id,
                'user_id' => Auth::id(), // 若是 console 或系統自動操作可能為 null
                'status' => $room->status,
                'message' => 'started_at:'.$room->started_at.',ended_at:'.$room->ended_at,
            ]);
        }
    }

    /**
     * Handle the Room "deleted" event.
     */
    public function deleted(Room $room): void
    {
        //
    }

    /**
     * Handle the Room "restored" event.
     */
    public function restored(Room $room): void
    {
        //
    }

    /**
     * Handle the Room "force deleted" event.
     */
    public function forceDeleted(Room $room): void
    {
        //
    }
}
