<?php
/**
 * VMO Booster Catalog — single source of truth for one-time token add-on packs.
 *
 * Defines every booster tier (price, token grant, Stripe price option key, label,
 * marketing copy, eligibility). All UI surfaces (my-plan page, settings, manager
 * admin), the checkout handler in vmo-ai-website-builder.php, and the Stripe
 * webhook credit logic read from this one file. Change a price or grant here
 * and it propagates everywhere automatically.
 *
 * Token grants are sized to slightly out-yield the equivalent dollars on a
 * recurring plan (~15% better) so boosters feel like a genuine bargain when a
 * user goes over their monthly buffer.
 *
 * @since 2026-05-30
 */
if (!defined('ABSPATH')) {
    exit;
}

class VMO_Booster_Catalog {

    /**
     * Get the full catalog of boosters.
     *
     * Schema per entry:
     *   key                : machine key (used as the 'topup' POST value)
     *   label              : human display name
     *   icon               : emoji used in UI cards
     *   price_usd          : retail price (USD)
     *   tokens             : token grant credited on successful payment
     *   price_option_key   : WP option name holding the Stripe Price ID
     *   category           : 'general' | 'phone' | 'ai'
     *   tagline            : short marketing line
     *   features           : array of bullet strings for plan card
     *   badge              : optional badge label (e.g. 'Best Value')
     *   never_expires      : boolean — boosters never expire by policy
     *   eligible_plans     : array of plan keys allowed to buy this booster; '*' = all paid plans
     *
     * @return array<string,array<string,mixed>>
     */
    public static function get_all() {
        $catalog = [
            'small' => [
                'key'              => 'small',
                'label'            => 'Small Add-On',
                'icon'             => '💎',
                'price_usd'        => 19.99,
                'tokens'           => 35000,
                'price_option_key' => 'vmo_price_topup_small',
                'category'         => 'general',
                'tagline'          => 'Quick boost for occasional needs',
                'features'         => [
                    'Immediate account boost',
                    'Never expires',
                    'Use anytime',
                    'Perfect for emergency needs',
                ],
                'badge'            => '',
                'never_expires'    => true,
                'eligible_plans'   => ['*'],
            ],
            'medium' => [
                'key'              => 'medium',
                'label'            => 'Medium Add-On',
                'icon'             => '💰',
                'price_usd'        => 49.99,
                'tokens'           => 90000,
                'price_option_key' => 'vmo_price_topup_medium',
                'category'         => 'general',
                'tagline'          => 'Popular choice for growing businesses',
                'features'         => [
                    'Immediate account boost',
                    'Best value',
                    'Never expires',
                    'Use anytime',
                ],
                'badge'            => 'Best Value',
                'never_expires'    => true,
                'eligible_plans'   => ['*'],
            ],
            'large' => [
                'key'              => 'large',
                'label'            => 'Large Add-On',
                'icon'             => '🚀',
                'price_usd'        => 99.99,
                'tokens'           => 180000,
                'price_option_key' => 'vmo_price_topup_large',
                'category'         => 'general',
                'tagline'          => 'Maximum boost for heavy campaigns',
                'features'         => [
                    'Immediate account boost',
                    'Maximum boost',
                    'Never expires',
                    'Perfect for big projects',
                ],
                'badge'            => '',
                'never_expires'    => true,
                'eligible_plans'   => ['*'],
            ],
            'phone_s' => [
                'key'              => 'phone_s',
                'label'            => 'Phone Pack — Small',
                'icon'             => '📞',
                'price_usd'        => 14.99,
                'tokens'           => 25000,
                'price_option_key' => 'vmo_manager_stripe_phone_pack_s_price_id',
                'category'         => 'phone',
                'tagline'          => 'Extra carrier minutes for busy days',
                'features'         => [
                    '~250 phone minutes',
                    'Never expires',
                    'Stacks with plan minutes',
                ],
                'badge'            => '',
                'never_expires'    => true,
                'eligible_plans'   => ['*'],
            ],
            'phone_m' => [
                'key'              => 'phone_m',
                'label'            => 'Phone Pack — Medium',
                'icon'             => '📞',
                'price_usd'        => 39.99,
                'tokens'           => 75000,
                'price_option_key' => 'vmo_manager_stripe_phone_pack_m_price_id',
                'category'         => 'phone',
                'tagline'          => 'Outbound campaign-friendly pack',
                'features'         => [
                    '~750 phone minutes',
                    'Never expires',
                    'Stacks with plan minutes',
                ],
                'badge'            => '',
                'never_expires'    => true,
                'eligible_plans'   => ['*'],
            ],
            'phone_l' => [
                'key'              => 'phone_l',
                'label'            => 'Phone Pack — Large',
                'icon'             => '📞',
                'price_usd'        => 79.99,
                'tokens'           => 160000,
                'price_option_key' => 'vmo_manager_stripe_phone_pack_l_price_id',
                'category'         => 'phone',
                'tagline'          => 'For multi-week outbound campaigns',
                'features'         => [
                    '~1,600 phone minutes',
                    'Never expires',
                    'Stacks with plan minutes',
                ],
                'badge'            => '',
                'never_expires'    => true,
                'eligible_plans'   => ['*'],
            ],
            'power_dialer' => [
                'key'              => 'power_dialer',
                'label'            => 'Power Dialer Pack',
                'icon'             => '⚡',
                'price_usd'        => 149.99,
                'tokens'           => 320000,
                'price_option_key' => 'vmo_manager_stripe_power_dialer_pack_price_id',
                'category'         => 'phone',
                'tagline'          => 'Heavy outbound sales sprints',
                'features'         => [
                    '~3,200 phone minutes',
                    'Power dialer mode enabled',
                    'Never expires',
                ],
                'badge'            => '',
                'never_expires'    => true,
                'eligible_plans'   => ['*'],
            ],
        ];

        /**
         * Filter the booster catalog. Use this to add, remove, or repackage tiers
         * without touching this file. Filter must return the full catalog array.
         */
        return apply_filters('vmo_booster_catalog', $catalog);
    }

    /**
     * Get a single booster by key. Returns null when the key is unknown.
     */
    public static function get($key) {
        $key = is_string($key) ? trim($key) : '';
        if ($key === '') {
            return null;
        }
        $catalog = self::get_all();
        return $catalog[$key] ?? null;
    }

    /**
     * Get only the boosters in a given category (general|phone|ai).
     */
    public static function get_by_category($category) {
        $category = is_string($category) ? trim($category) : '';
        $out = [];
        foreach (self::get_all() as $key => $entry) {
            if (($entry['category'] ?? '') === $category) {
                $out[$key] = $entry;
            }
        }
        return $out;
    }

    /**
     * Get the token grant for a booster key, or 0 if unknown.
     */
    public static function get_token_grant($key) {
        $entry = self::get($key);
        return $entry ? (int) ($entry['tokens'] ?? 0) : 0;
    }

    /**
     * Get the configured Stripe Price ID for a booster key, or '' when not set.
     */
    public static function get_price_id($key) {
        $entry = self::get($key);
        if (!$entry) {
            return '';
        }
        $opt = (string) ($entry['price_option_key'] ?? '');
        return $opt === '' ? '' : trim((string) get_option($opt, ''));
    }

    /**
     * Build the topup -> price_option_key map used by the checkout handler.
     * Kept here so adding a tier means editing this one file.
     */
    public static function get_price_option_map() {
        $map = [];
        foreach (self::get_all() as $key => $entry) {
            $map[$key] = (string) ($entry['price_option_key'] ?? '');
        }
        return $map;
    }

    /**
     * Is this booster available to the given plan?
     */
    public static function is_eligible_for_plan($booster_key, $plan_key) {
        $entry = self::get($booster_key);
        if (!$entry) {
            return false;
        }
        $eligible = (array) ($entry['eligible_plans'] ?? ['*']);
        if (in_array('*', $eligible, true)) {
            return true;
        }
        return in_array((string) $plan_key, $eligible, true);
    }
}

if (!function_exists('vmo_get_booster_catalog')) {
    function vmo_get_booster_catalog() {
        return VMO_Booster_Catalog::get_all();
    }
}

if (!function_exists('vmo_get_booster_token_grant')) {
    function vmo_get_booster_token_grant($key) {
        return VMO_Booster_Catalog::get_token_grant($key);
    }
}
