<?php

namespace App\Observers;

use App\Models\Maintenance;
use App\Services\SystemNotificationService;

class MaintenanceObserver
{
    public function created(Maintenance $maintenance): void
    {
        if (! $maintenance->hotel_id) {
            return;
        }

        app(SystemNotificationService::class)->notifyHotel(
            (int) $maintenance->hotel_id,
            'Maintenance request created',
            'Maintenance "' . ($maintenance->title ?: ('#' . $maintenance->id)) . '" was created.',
            [
                'type' => 'maintenance_created',
                'url' => '/maintenance',
                'context' => [
                    'maintenance_id' => $maintenance->id,
                    'priority' => $maintenance->priority,
                    'status' => $maintenance->status,
                ],
            ]
        );
    }

    public function updated(Maintenance $maintenance): void
    {
        if (! $maintenance->hotel_id || ! $maintenance->wasChanged('status')) {
            return;
        }

        app(SystemNotificationService::class)->notifyHotel(
            (int) $maintenance->hotel_id,
            'Maintenance status updated',
            'Maintenance "' . ($maintenance->title ?: ('#' . $maintenance->id)) . '" is now ' . $maintenance->status . '.',
            [
                'type' => 'maintenance_status',
                'url' => '/maintenance',
                'context' => [
                    'maintenance_id' => $maintenance->id,
                    'status' => $maintenance->status,
                ],
            ]
        );
    }
}

