<?php
/**
 * VMO Pricing Policy — single source of truth for plans, markups, and economic floors.
 *
 * Confirmed by Founder May 2026:
 *  - Direct prices: Tester $49.99 / Starter $99.99 / Pro $199.99 / Elite $499.99 / Enterprise $999.99
 *  - QuickLaunch (QL) adds +$50/mo to every plan
 *  - Annual = monthly × 11 (one free month). TESTER HAS NO ANNUAL OPTION.
 *  - Net profit floor: >= 25% of gross revenue, every plan, every billing cycle
 *  - Overhead floor: >= 35% (can be used to compensate annual shortfall but never below 35%)
 *  - Markups: 12x standard AI / 20x tiny AI (+$0.10 floor) / 5x Sora / 2.5x phone & carrier
 *  - QL hosting: every user starts on $6 Vultr Starter (vc2-1c-1gb)
 *  - Hosting upgrades: 1.5x Vultr cost (Business $9, Pro $18, Performance $36, High-Perf $72)
 *  - Affiliate commission: 5% of gross
 *  - 1 VMO Token = $0.000667 (legacy rate preserved for backward compatibility)
 *
 * Annual mechanic that hits the 25% floor:
 *  - Overhead allocation flips from "% × Direct base" (monthly) to a fixed
 *    35% × gross (annual). This is the overhead floor, never below.
 *  - AI budget per-month equivalent on annual is within ~2% of monthly — no
 *    operation-catalog changes are needed between monthly and annual users.
 *
 * @package VMO_AI_Website_Builder
 * @since   1.0.60
 */

if (!defined('ABSPATH')) exit;

// ---------------------------------------------------------------------------
// Core economic constants
// ---------------------------------------------------------------------------

if (!defined('VMO_TOKEN_RATE')) {
    define('VMO_TOKEN_RATE', 0.000667); // 1 token = $0.000667 (1500 tokens = $1)
}

if (!defined('VMO_MIN_NET_PROFIT_RATE')) {
    define('VMO_MIN_NET_PROFIT_RATE', 0.25); // 25% net profit floor
}

if (!defined('VMO_OVERHEAD_FLOOR_RATE')) {
    define('VMO_OVERHEAD_FLOOR_RATE', 0.35); // 35% overhead floor (never below)
}

if (!defined('VMO_DEFAULT_AFFILIATE_COMMISSION')) {
    define('VMO_DEFAULT_AFFILIATE_COMMISSION', 5.00); // 5% of gross
}

if (!defined('VMO_PROVIDER_COST_BUDGET_RATE')) {
    define('VMO_PROVIDER_COST_BUDGET_RATE', 0.30); // AI provider budget ceiling
}

// ---------------------------------------------------------------------------
// Markup rules (applied to provider cost)
// ---------------------------------------------------------------------------

if (!defined('VMO_MARKUP_STANDARD'))  define('VMO_MARKUP_STANDARD', 12.0);  // Standard AI
if (!defined('VMO_MARKUP_TINY'))      define('VMO_MARKUP_TINY', 20.0);      // Tiny AI ops
if (!defined('VMO_MARKUP_TINY_FLOOR_USD')) define('VMO_MARKUP_TINY_FLOOR_USD', 0.10); // $0.10 floor for tiny ops
if (!defined('VMO_MARKUP_SORA'))      define('VMO_MARKUP_SORA', 5.0);       // Sora 2 Pro
if (!defined('VMO_MARKUP_PHONE'))     define('VMO_MARKUP_PHONE', 2.5);      // Twilio / carrier
if (!defined('VMO_MARKUP_HOSTING'))   define('VMO_MARKUP_HOSTING', 1.5);    // Vultr resize tiers

// ---------------------------------------------------------------------------
// Plan price table (single source of truth)
// ---------------------------------------------------------------------------

if (!function_exists('vmo_pricing_plan_table')) {
    /**
     * Returns the canonical plan table.
     *
     * direct_monthly  = monthly price for direct-install customers
     * direct_annual   = monthly × 11 (one free month). NULL for tester.
     * ql_monthly      = direct_monthly + $50 uplift
     * ql_annual       = ql_monthly × 11. NULL for tester.
     * monthly_only    = true => no annual SKU
     */
    function vmo_pricing_plan_table() {
        $rows = [
            'tester' => [
                'label'          => 'Tester',
                'direct_monthly' => 49.99,
                'direct_annual'  => null,
                'ql_monthly'     => 99.99,
                'ql_annual'      => null,
                'monthly_only'   => true,
                'ideal_for'      => 'Capability testing, demos',
            ],
            'starter' => [
                'label'          => 'Starter',
                'direct_monthly' => 99.99,
                'direct_annual'  => 1099.89, // 99.99 * 11
                'ql_monthly'     => 149.99,
                'ql_annual'      => 1649.89, // 149.99 * 11
                'monthly_only'   => false,
                'ideal_for'      => 'Side hustles, solo operators',
            ],
            'pro' => [
                'label'          => 'Pro',
                'direct_monthly' => 199.99,
                'direct_annual'  => 2199.89, // 199.99 * 11
                'ql_monthly'     => 249.99,
                'ql_annual'      => 2749.89, // 249.99 * 11
                'monthly_only'   => false,
                'ideal_for'      => 'Serious business owners',
                'recommended'    => true,
            ],
            'elite' => [
                'label'          => 'Elite',
                'direct_monthly' => 499.99,
                'direct_annual'  => 5499.89, // 499.99 * 11
                'ql_monthly'     => 549.99,
                'ql_annual'      => 6049.89, // 549.99 * 11
                'monthly_only'   => false,
                'ideal_for'      => 'Heavy users, aggressive growth',
            ],
            'enterprise' => [
                'label'          => 'Enterprise',
                'direct_monthly' => 999.99,
                'direct_annual'  => 10999.89, // 999.99 * 11
                'ql_monthly'     => 1049.99,
                'ql_annual'      => 11549.89, // 1049.99 * 11
                'monthly_only'   => false,
                'ideal_for'      => 'Agencies, multi-tenant',
            ],
        ];

        return apply_filters('vmo_pricing_plan_table', $rows);
    }
}

if (!function_exists('vmo_get_plan_price')) {
    /**
     * Resolve a price from the canonical table.
     *
     * @param string $plan_key      One of tester|starter|pro|elite|enterprise
     * @param string $channel       'direct' or 'ql'
     * @param string $billing_cycle 'monthly' or 'annual'
     * @return float|null Price in USD, or null if not available (e.g. tester annual)
     */
    function vmo_get_plan_price($plan_key, $channel = 'direct', $billing_cycle = 'monthly') {
        $table = vmo_pricing_plan_table();
        if (!isset($table[$plan_key])) return null;
        $row = $table[$plan_key];
        $key = ($channel === 'ql' ? 'ql_' : 'direct_') . ($billing_cycle === 'annual' ? 'annual' : 'monthly');
        return isset($row[$key]) ? $row[$key] : null;
    }
}

if (!function_exists('vmo_plan_supports_annual')) {
    function vmo_plan_supports_annual($plan_key) {
        $table = vmo_pricing_plan_table();
        return isset($table[$plan_key]) && empty($table[$plan_key]['monthly_only']);
    }
}

// ---------------------------------------------------------------------------
// Overhead schedule
// ---------------------------------------------------------------------------

if (!function_exists('vmo_get_overhead_rate')) {
    /**
     * Returns overhead allocation as fraction of GROSS revenue.
     *
     * Monthly:
     *   - Direct: 35% × Direct base / gross  (gross == direct base, so 35%)
     *   - QL Tester-Elite: 40% × Direct base / QL gross
     *   - QL Enterprise: 35% × Direct base / QL gross
     * Annual (all plans): 35% × gross (flat floor)
     */
    function vmo_get_overhead_rate($plan_key, $channel = 'direct', $billing_cycle = 'monthly') {
        // Annual always at the 35% gross floor
        if ($billing_cycle === 'annual') {
            return VMO_OVERHEAD_FLOOR_RATE;
        }

        $direct_base = vmo_get_plan_price($plan_key, 'direct', 'monthly');
        $gross       = vmo_get_plan_price($plan_key, $channel, 'monthly');

        if (!$direct_base || !$gross) {
            return VMO_OVERHEAD_FLOOR_RATE;
        }

        if ($channel === 'ql') {
            $overhead_pct_of_direct = ($plan_key === 'enterprise') ? 0.35 : 0.40;
        } else {
            $overhead_pct_of_direct = 0.35;
        }

        $overhead_usd = $direct_base * $overhead_pct_of_direct;
        $rate         = $overhead_usd / $gross;

        // Floor: never below 35% of gross
        return max($rate, VMO_OVERHEAD_FLOOR_RATE);
    }
}

// ---------------------------------------------------------------------------
// Markup helpers
// ---------------------------------------------------------------------------

if (!function_exists('vmo_apply_markup')) {
    /**
     * Apply a markup rule to a raw provider cost to derive the user-facing dollar charge.
     *
     * @param float  $provider_cost_usd Raw provider cost in USD
     * @param string $markup_rule       One of: standard|tiny|sora|phone|hosting
     * @return float User charge in USD
     */
    function vmo_apply_markup($provider_cost_usd, $markup_rule = 'standard') {
        $cost = max(0.0, (float) $provider_cost_usd);
        switch ($markup_rule) {
            case 'tiny':
                return max($cost * VMO_MARKUP_TINY, VMO_MARKUP_TINY_FLOOR_USD);
            case 'sora':
                return $cost * VMO_MARKUP_SORA;
            case 'phone':
                return $cost * VMO_MARKUP_PHONE;
            case 'hosting':
                return $cost * VMO_MARKUP_HOSTING;
            case 'standard':
            default:
                return $cost * VMO_MARKUP_STANDARD;
        }
    }
}

if (!function_exists('vmo_dollars_to_tokens')) {
    /**
     * Convert a USD amount to VMO tokens using the canonical rate.
     */
    function vmo_dollars_to_tokens($usd) {
        $usd = max(0.0, (float) $usd);
        return (int) ceil($usd / VMO_TOKEN_RATE);
    }
}

if (!function_exists('vmo_tokens_to_dollars')) {
    function vmo_tokens_to_dollars($tokens) {
        return (float) $tokens * VMO_TOKEN_RATE;
    }
}

// ---------------------------------------------------------------------------
// Vultr hosting tiers (1.5x markup)
// ---------------------------------------------------------------------------

if (!function_exists('vmo_pricing_hosting_tiers')) {
    /**
     * Hosting tier catalog.
     *
     * starter is the base everyone signs up on (no upgrade markup).
     * business/pro/performance/high_perf are user-paid upgrades at 1.5x Vultr cost.
     */
    function vmo_pricing_hosting_tiers() {
        $tiers = [
            'starter' => [
                'label'         => 'Starter Hosting',
                'plan'          => 'vc2-1c-1gb',
                'specs'         => '1 vCPU, 1 GB RAM, 25 GB SSD',
                'provider_cost' => 6.00,
                'user_price'    => 6.00,  // bundled in plan, no upgrade markup
                'is_default'    => true,
            ],
            'business' => [
                'label'         => 'Business Hosting',
                'plan'          => 'vc2-1c-2gb',
                'specs'         => '1 vCPU, 2 GB RAM, 55 GB SSD',
                'provider_cost' => 6.00,
                'user_price'    => 9.00,  // 6 × 1.5
            ],
            'pro' => [
                'label'         => 'Pro Hosting',
                'plan'          => 'vc2-1c-2gb-amd',
                'specs'         => '1 vCPU AMD, 2 GB RAM, 55 GB SSD',
                'provider_cost' => 12.00,
                'user_price'    => 18.00, // 12 × 1.5
            ],
            'performance' => [
                'label'         => 'Performance Hosting',
                'plan'          => 'vc2-2c-4gb',
                'specs'         => '2 vCPU, 4 GB RAM, 80 GB SSD',
                'provider_cost' => 24.00,
                'user_price'    => 36.00, // 24 × 1.5
            ],
            'high_perf' => [
                'label'         => 'High-Performance Hosting',
                'plan'          => 'vhf-2c-4gb',
                'specs'         => '2 vCPU HF, 4 GB RAM, 128 GB SSD',
                'provider_cost' => 48.00,
                'user_price'    => 72.00, // 48 × 1.5
            ],
        ];
        return apply_filters('vmo_pricing_hosting_tiers', $tiers);
    }
}

if (!function_exists('vmo_get_default_hosting_tier_key')) {
    /** All QuickLaunch users start on $6 Vultr Starter. */
    function vmo_get_default_hosting_tier_key() {
        return 'starter';
    }
}

// ---------------------------------------------------------------------------
// QuickLaunch uplift constant
// ---------------------------------------------------------------------------

if (!defined('VMO_QL_HOSTED_MONTHLY_UPLIFT_USD')) {
    define('VMO_QL_HOSTED_MONTHLY_UPLIFT_USD', 50.00);
}
