<?php
namespace EasyChat\Library\Queue\Jobs;

use EasyChat\Library\Messenger\ScheduledMessageService;
use EasyChat\Library\Queue\JobInterface;

class ScheduledMessageReleaseJob implements JobInterface {
    private $registry;

    public function __construct($registry) {
        $this->registry = $registry;
    }

    public function handle(array $payload): void {
        $id = max(0, (int)($payload['scheduled_message_id'] ?? 0));
        if ($id <= 0) {
            throw new \InvalidArgumentException('scheduled_message_id_required');
        }
        $result = (new ScheduledMessageService($this->registry))->release($id);
        if (empty($result['success']) && empty($result['terminal'])) {
            throw new \RuntimeException((string)($result['error'] ?? 'scheduled_message_release_failed'));
        }
    }
}
