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

use EasyChat\Library\AttachmentStorage;
use EasyChat\Library\Queue\JobInterface;

 
class ChatAttachmentCleanupJob implements JobInterface {
    private $registry;

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

    public function handle(array $payload): void {
        $ids = array_values(array_unique(array_filter(array_map('intval', (array)($payload['chat_ids'] ?? [])))));
        if (!$ids || count($ids) > 100) {
            throw new \InvalidArgumentException('chat_attachment_cleanup_payload_invalid');
        }
        $storage = new AttachmentStorage($this->registry);
        foreach ($ids as $chat_id) {
            if (!$storage->deleteChatDirectory($chat_id)) {
                throw new \RuntimeException('chat_attachment_cleanup_failed');
            }
        }
    }
}
