<?php

namespace App\Observers;

use App\Models\ServiceRequest;
use App\Services\SystemNotificationService;

class ServiceRequestObserver
{
    public function created(ServiceRequest $serviceRequest): void
    {
        if (! $serviceRequest->hotel_id) {
            return;
        }

        app(SystemNotificationService::class)->notifyHotel(
            (int) $serviceRequest->hotel_id,
            'New service request',
            'Service request ' . ($serviceRequest->request_number ?: ('#' . $serviceRequest->id)) . ' was created.',
            [
                'type' => 'service_request_created',
                'url' => '/services/requests',
                'context' => [
                    'service_request_id' => $serviceRequest->id,
                    'status' => $serviceRequest->status,
                ],
            ]
        );
    }

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

        app(SystemNotificationService::class)->notifyHotel(
            (int) $serviceRequest->hotel_id,
            'Service request status updated',
            'Service request ' . ($serviceRequest->request_number ?: ('#' . $serviceRequest->id)) . ' is now ' . $serviceRequest->status . '.',
            [
                'type' => 'service_request_status',
                'url' => '/services/requests',
                'context' => [
                    'service_request_id' => $serviceRequest->id,
                    'status' => $serviceRequest->status,
                ],
            ]
        );
    }
}

