<?php

namespace App\Observers;

use App\Models\Leave;
use App\Services\SystemNotificationService;

class LeaveObserver
{
    public function created(Leave $leave): void
    {
        if (! $leave->hotel_id) {
            return;
        }

        app(SystemNotificationService::class)->notifyHotel(
            (int) $leave->hotel_id,
            'New leave application',
            'Leave request #' . $leave->id . ' has been submitted.',
            [
                'type' => 'leave_created',
                'url' => '/leave',
                'context' => [
                    'leave_id' => $leave->id,
                    'status' => $leave->status,
                    'staff_id' => $leave->staff_id,
                ],
            ]
        );
    }

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

        app(SystemNotificationService::class)->notifyHotel(
            (int) $leave->hotel_id,
            'Leave status updated',
            'Leave request #' . $leave->id . ' is now ' . $leave->status . '.',
            [
                'type' => 'leave_status',
                'url' => '/leave',
                'context' => [
                    'leave_id' => $leave->id,
                    'status' => $leave->status,
                ],
            ]
        );
    }
}

