<?php
namespace EasyChat\Library\Messenger;

require_once dirname(__DIR__) . '/attachment_policy.php';
require_once __DIR__ . '/channel_catalog.php';

use EasyChat\Library\AttachmentPolicy;








class AttachmentCapabilities {

    public static function forProvider(string $provider_code): array {
        $provider_code = self::normalizeProvider($provider_code);
        $documents = [
            'image_extensions' => AttachmentPolicy::allowedImageExtensions(),
            'documents' => true,
            'document_extensions' => AttachmentPolicy::allowedDocumentExtensions(),
             
             
            'pdf' => true,
        ];
        switch ($provider_code) {
            case 'widget':
                return array_merge(['images' => true, 'max_attachments' => 10, 'max_documents' => 1], $documents);
            case 'telegram':
                return array_merge(['images' => true, 'max_attachments' => 10], $documents);
            case 'avito':
                return array_merge(['images' => true, 'max_attachments' => 10, 'document_delivery' => 'signed_link'], $documents);
            case 'max':
            case 'max-bot':
                return array_merge([
                    'images' => true,
                    'max_attachments' => 10,
                    'max_documents' => 1,
                    'mixed_image_document' => false,
                ], $documents);
            case 'whatsapp-personal':
                return array_merge([
                    'images' => true,
                    'max_attachments' => 10,
                    'max_documents' => 10,
                    'mixed_image_document' => true,
                ], $documents);
            case 'vk':
                return array_merge(['images' => true, 'max_attachments' => 10], $documents);
            case 'youla':
                return array_merge(['images' => true, 'max_attachments' => 10, 'document_delivery' => 'signed_link'], $documents);
            case 'wildberries':
                return [
                    'images' => true,
                    'image_extensions' => ['jpg', 'jpeg', 'png'],
                    'documents' => true,
                    'document_extensions' => ['pdf'],
                    'pdf' => true,
                    'max_attachments' => 10,
                ];
            case 'ozon':
                return array_merge(['images' => true, 'max_attachments' => 10], $documents);
            case 'yandex_market':
                return array_merge([
                    'images' => true,
                    'max_attachments' => 10,
                    'max_file_size_mb' => 5,
                ], $documents);
            default:
                return [
                    'images' => false,
                    'image_extensions' => [],
                    'documents' => false,
                    'document_extensions' => [],
                    'pdf' => false,
                    'max_attachments' => 0,
                ];
        }
    }

    public static function isKindAllowed(string $provider_code, string $kind): bool {
        $capabilities = self::forProvider($provider_code);
        $kind = strtolower(trim($kind));
        if ($kind === 'document' || $kind === 'pdf') {
            return !empty($capabilities['documents']);
        }
        if ($kind === 'image') {
            return !empty($capabilities['images']);
        }
        return false;
    }

    public static function clientMatrix(): array {
        $providers = ChannelCatalog::filterOrder();
        $matrix = [];
        foreach ($providers as $provider) {
            $matrix[$provider] = self::forProvider($provider);
        }
        return $matrix;
    }

    private static function normalizeProvider(string $provider_code): string {
        $provider_code = strtolower(preg_replace('/[^a-z0-9_\-]/i', '', $provider_code));
        return $provider_code !== '' && $provider_code !== 'all' ? $provider_code : 'widget';
    }
}
