<?php

namespace App\Services\Subscriptions;

use App\Models\SubscriptionPlan;

/**
 * قدرات الخطة (ميزات مدفوعة) — تُقرأ من JSON وتُدمج مع افتراضات آمنة حسب slug.
 */
final class PlanCapabilities
{
    /**
     * @return array<string, mixed>
     */
    public static function forPlan(SubscriptionPlan $plan): array
    {
        $features = $plan->features ?? [];
        $fromDb = $features['capabilities'] ?? null;

        $defaults = self::defaultsForSlug((string) $plan->slug);

        if (! is_array($fromDb)) {
            return $defaults;
        }

        return array_merge($defaults, $fromDb);
    }

    /**
     * @return array<string, mixed>
     */
    private static function defaultsForSlug(string $slug): array
    {
        return match ($slug) {
            'pro' => [
                'text_analysis' => true,
                'text_analysis_daily_limit' => 30,
                'text_analysis_max_words' => 5000,
                'text_analysis_unlimited' => false,
                'text_analysis_export' => false,
                'image_generation' => true,
                'image_generation_daily_limit' => 15,
                'pdf_export' => true,
                'voice_ai' => false,
                'memory_ai' => false,
                'file_analysis' => true,
                'file_intelligence' => true,
                'file_intelligence_daily_limit' => 25,
                'ai_workspace' => true,
                'copy_long_text' => true,
                'save_attachments' => true,
                'share_premium' => true,
                'speed_tier' => 'fast',
                'model_tier' => 'strong',
                'reward_multiplier' => 1.5,
            ],
            'ultra' => [
                'text_analysis' => true,
                'text_analysis_daily_limit' => null,
                'text_analysis_max_words' => null,
                'text_analysis_unlimited' => true,
                'text_analysis_export' => true,
                'image_generation' => true,
                'image_generation_daily_limit' => null,
                'pdf_export' => true,
                'voice_ai' => true,
                'memory_ai' => true,
                'file_analysis' => true,
                'file_intelligence' => true,
                'file_intelligence_daily_limit' => null,
                'ai_workspace' => true,
                'copy_long_text' => true,
                'save_attachments' => true,
                'share_premium' => true,
                'speed_tier' => 'fastest',
                'model_tier' => 'flagship',
                'reward_multiplier' => 2.0,
            ],
            default => [
                'text_analysis' => true,
                'text_analysis_daily_limit' => 5,
                'text_analysis_max_words' => 500,
                'text_analysis_unlimited' => false,
                'text_analysis_export' => false,
                'image_generation' => false,
                'pdf_export' => false,
                'voice_ai' => false,
                'memory_ai' => false,
                'file_analysis' => false,
                'file_intelligence' => false,
                'ai_workspace' => false,
                'copy_long_text' => false,
                'save_attachments' => false,
                'share_premium' => false,
                'speed_tier' => 'normal',
                'model_tier' => 'standard',
                'reward_multiplier' => 1.0,
            ],
        };
    }
}
