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

use EasyChat\Library\AttachmentStorage;
use Exception;

class ImageVariantJob {

    private $registry;

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

    public function handle(array $payload): void {
        $chat_id = (int)($payload['chat_id'] ?? 0);
        $file_name = basename(trim((string)($payload['file_name'] ?? '')));
        $attachment_id = (int)($payload['attachment_id'] ?? 0);

        if ($chat_id <= 0 || $file_name === '') {
            throw new Exception('image_variant_payload_invalid');
        }

        $storage = new AttachmentStorage($this->registry);
        $result = $storage->generateVariantsForFile($chat_id, $file_name, $attachment_id);
        $status = (string)($result['status'] ?? 'failed');
        if (!in_array($status, ['ready', 'skipped'], true)) {
             
             
            throw new Exception('image_variant_' . preg_replace('/[^a-z0-9_-]/i', '', $status));
        }
    }
}
