<?php

namespace App\Actions\Leave;

use App\Contracts\LeaveRepositoryInterface;
use App\Models\LeaveApplication;

class RejectLeaveAction
{
    public function __construct(
        protected LeaveRepositoryInterface $leaveRepository
    ) {}

    public function execute(LeaveApplication $application, int $approverId, string $rejectionReason): bool
    {
        if ($application->status !== 'pending') {
            return false;
        }

        $updated = $this->leaveRepository->updateStatus($application, 'rejected', $approverId, $rejectionReason);

        if ($updated) {
            $this->leaveRepository->addHistory($application->id, $approverId, 'reject', "Alasan: {$rejectionReason}");
            \App\Events\LeaveStatusUpdatedEvent::dispatch($application, 'rejected', $rejectionReason);
        }

        return $updated;
    }
}
