<?php

namespace App\Observers\Hotel;

use Illuminate\Support\Str;


use App\Models\Hotel\Room;
use App\Models\Hotel\Reservation;

use App\Helpers\Environment\SystemFeaturesHelper;

class RoomObserver
{
    public function saving(Room $room): void
    {
        // A=Ensure External UUID on ExternalConnections-BookingEngine
        if (
            SystemFeaturesHelper::getGuestFeatureStatus('external_integrations')
            && empty($room->ical_token)
        ) {
            $room->ical_token = (string) Str::uuid();
        }
    }

    public function deleting(Room $room): void
    {
        // Avoid Delete Unassigned Room
        if (in_array($room->id, [1]))
            throw new \Exception("Unassigned Room cannot be Deleted.");

        // Save Relationships
        Reservation::where('room_id', $room->id)->update(['roomType_id' => 2, 'room_id' => 1]);
    }
}
