<?php
/**
 * VMO Operation Catalog — single source of truth for per-operation pricing.
 *
 * Every billable AI / provider operation is registered here once.
 * Charges are derived from provider cost × markup rule (locked in vmo-pricing-policy.php).
 *
 * Rules (May 2026):
 *  - Standard AI: 12× provider cost
 *  - Tiny AI: 20× provider cost, min $0.10
 *  - Sora 2 Pro / Standard: 5× provider cost
 *  - Phone / SMS / carrier: 2.5× provider cost
 *  - Hosting resize: 1.5× Vultr cost (handled by hosting tier table, not catalog)
 *
 * Each entry also returns the derived VMO token count using the canonical
 * rate (1 token = $0.000667) so legacy callers continue to work without
 * having to be refactored individually.
 *
 * @package VMO_AI_Website_Builder
 * @since   1.0.60
 */

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

// Make sure the pricing policy (markups, token rate) is loaded.
require_once __DIR__ . '/vmo-pricing-policy.php';

if (!function_exists('vmo_operation_catalog')) {
    /**
     * Canonical catalog. Keyed by operation key.
     *
     * provider_cost = average raw USD cost charged to VMO by the upstream provider
     * markup        = 'standard' | 'tiny' | 'sora' | 'phone'
     * unit          = human-readable unit (call|min|message|image|second|page|request)
     * notes         = optional notes for audit
     */
    function vmo_operation_catalog() {
        $catalog = [
            // ─── Content generation ────────────────────────────────────────
            'full_page'              => ['provider_cost' => 0.0833, 'markup' => 'standard', 'unit' => 'page',    'notes' => 'Avg full webpage gen'],
            'content'                => ['provider_cost' => 0.0833, 'markup' => 'standard', 'unit' => 'page'],
            'section'                => ['provider_cost' => 0.0167, 'markup' => 'standard', 'unit' => 'section'],
            'refinement'             => ['provider_cost' => 0.0075, 'markup' => 'standard', 'unit' => 'refinement'],

            // ─── Images ────────────────────────────────────────────────────
            'image'                  => ['provider_cost' => 0.0600, 'markup' => 'standard', 'unit' => 'image',   'notes' => 'GPT Image 1 standard'],
            'gpt_image'              => ['provider_cost' => 0.0600, 'markup' => 'standard', 'unit' => 'image'],
            'dalle'                  => ['provider_cost' => 0.0600, 'markup' => 'standard', 'unit' => 'image'],
            'dalle_image'            => ['provider_cost' => 0.0600, 'markup' => 'standard', 'unit' => 'image'],

            // ─── Video (Sora) ──────────────────────────────────────────────
            'sora_video_4s'          => ['provider_cost' => 2.40, 'markup' => 'sora', 'unit' => '4s_video'],
            'video_4s'               => ['provider_cost' => 2.40, 'markup' => 'sora', 'unit' => '4s_video'],
            'sora_video_8s'          => ['provider_cost' => 4.80, 'markup' => 'sora', 'unit' => '8s_video'],
            'video_8s'               => ['provider_cost' => 4.80, 'markup' => 'sora', 'unit' => '8s_video'],
            'sora_video_12s'         => ['provider_cost' => 7.20, 'markup' => 'sora', 'unit' => '12s_video'],
            'video_12s'              => ['provider_cost' => 7.20, 'markup' => 'sora', 'unit' => '12s_video'],
            'sora_std_4s'            => ['provider_cost' => 1.20, 'markup' => 'sora', 'unit' => '4s_video_std'],
            'sora_std_8s'            => ['provider_cost' => 2.40, 'markup' => 'sora', 'unit' => '8s_video_std'],
            'sora_std_12s'           => ['provider_cost' => 3.60, 'markup' => 'sora', 'unit' => '12s_video_std'],

            // ─── Effects & animation ───────────────────────────────────────
            'effects'                => ['provider_cost' => 0.0058, 'markup' => 'standard', 'unit' => 'effect'],
            'animation'              => ['provider_cost' => 0.0058, 'markup' => 'standard', 'unit' => 'effect'],

            // ─── Chat / conversation ───────────────────────────────────────
            'chat'                   => ['provider_cost' => 0.0028, 'markup' => 'tiny',     'unit' => 'message', 'notes' => 'gpt-5.5-nano single turn'],
            'website_chat'           => ['provider_cost' => 0.0083, 'markup' => 'standard', 'unit' => 'message'],
            'consultation'           => ['provider_cost' => 0.0225, 'markup' => 'standard', 'unit' => 'message'],
            'consultation_message'   => ['provider_cost' => 0.0225, 'markup' => 'standard', 'unit' => 'message'],
            'ai_recommendations'     => ['provider_cost' => 0.0333, 'markup' => 'standard', 'unit' => 'recommendation'],
            'voice_message'          => ['provider_cost' => 0.0208, 'markup' => 'standard', 'unit' => 'message'],
            'info_extraction'        => ['provider_cost' => 0.0167, 'markup' => 'standard', 'unit' => 'extraction'],
            'business_summary'       => ['provider_cost' => 0.0333, 'markup' => 'standard', 'unit' => 'summary'],
            'sales_script'           => ['provider_cost' => 0.0120, 'markup' => 'standard', 'unit' => 'script',  'notes' => 'Cold-call sales script (~1200 tok)'],
            'contact_summary'        => ['provider_cost' => 0.0070, 'markup' => 'standard', 'unit' => 'summary', 'notes' => 'CRM contact AI relationship summary'],

            // ─── Business wizard / website generation ──────────────────────
            'business_wizard'        => ['provider_cost' => 0.1667, 'markup' => 'standard', 'unit' => 'session', 'notes' => 'Full wizard turn ~$0.17'],
            'generate_website'       => ['provider_cost' => 0.7000, 'markup' => 'standard', 'unit' => 'website', 'notes' => 'Legacy template build (templates + 1 LLM call/page)'],
            'website_generation'     => ['provider_cost' => 0.7000, 'markup' => 'standard', 'unit' => 'website'],
            // Full premium build: 18 supreme design agents (orchestrated LLM calls) +
            // richer per-page prompts + model QA critic. Real provider cost ~$2.60.
            // Standard 12× → ~$31.20 user list price (~46,777 tokens). At INITIAL setup
            // this is bonus-discounted down to the legacy generate_website charge
            // (see vmo-initial-setup-policy.php); RERUNS pay this full price.
            'generate_website_premium'   => ['provider_cost' => 2.6000, 'markup' => 'standard', 'unit' => 'website', 'notes' => 'Full 18-agent premium site build'],
            'website_generation_premium' => ['provider_cost' => 2.6000, 'markup' => 'standard', 'unit' => 'website'],
            'website_preview'        => ['provider_cost' => 0.2778, 'markup' => 'standard', 'unit' => 'preview'],
            'website_analysis'       => ['provider_cost' => 0.0222, 'markup' => 'standard', 'unit' => 'analysis'],
            'ai_qa_evaluation'       => ['provider_cost' => 0.0444, 'markup' => 'standard', 'unit' => 'eval'],

            // ─── TTS / transcription / vision ──────────────────────────────
            'tts'                    => ['provider_cost' => 0.0045, 'markup' => 'tiny',     'unit' => 'utterance'],
            'tts_response'           => ['provider_cost' => 0.0045, 'markup' => 'tiny',     'unit' => 'utterance'],
            'greeting_audio'         => ['provider_cost' => 0.0210, 'markup' => 'standard', 'unit' => 'greeting'],
            'tts_long'               => ['provider_cost' => 0.0300, 'markup' => 'standard', 'unit' => '1k_chars'],
            'tts_per_1k'             => ['provider_cost' => 0.0300, 'markup' => 'standard', 'unit' => '1k_chars'],
            'transcription'          => ['provider_cost' => 0.0060, 'markup' => 'tiny',     'unit' => 'minute'],
            'whisper'                => ['provider_cost' => 0.0060, 'markup' => 'tiny',     'unit' => 'minute'],
            'vision'                 => ['provider_cost' => 0.0150, 'markup' => 'standard', 'unit' => 'image'],
            'image_analysis'         => ['provider_cost' => 0.0150, 'markup' => 'standard', 'unit' => 'image'],

            // ─── Deep research ─────────────────────────────────────────────
            'deep_research'          => ['provider_cost' => 1.0000, 'markup' => 'standard', 'unit' => 'research', 'notes' => 'GPT-5.5 multi-step research'],
            'research'               => ['provider_cost' => 1.0000, 'markup' => 'standard', 'unit' => 'research'],

            // ─── Documents & contacts ──────────────────────────────────────
            'document'               => ['provider_cost' => 0.0300, 'markup' => 'standard', 'unit' => 'document'],
            'document_analysis'      => ['provider_cost' => 0.0300, 'markup' => 'standard', 'unit' => 'document'],
            'email_campaign'         => ['provider_cost' => 0.0700, 'markup' => 'standard', 'unit' => 'campaign'],
            'email_campaign_generation' => ['provider_cost' => 0.0700, 'markup' => 'standard', 'unit' => 'campaign'],
            'contact_insights'       => ['provider_cost' => 0.0300, 'markup' => 'standard', 'unit' => 'insight'],
            'contact_recommendations'=> ['provider_cost' => 0.0300, 'markup' => 'standard', 'unit' => 'insight'],
            'lead_discovery'         => ['provider_cost' => 0.1000, 'markup' => 'standard', 'unit' => 'discovery'],

            // ─── Recurring (weekly/monthly automated) ──────────────────────
            'weekly_deep_research'   => ['provider_cost' => 0.0500, 'markup' => 'tiny', 'unit' => 'scan'],
            'weekly_research'        => ['provider_cost' => 0.0500, 'markup' => 'tiny', 'unit' => 'scan'],
            'weekly_email_research'  => ['provider_cost' => 0.1000, 'markup' => 'tiny', 'unit' => 'scan'],
            'email_research'         => ['provider_cost' => 0.1000, 'markup' => 'tiny', 'unit' => 'scan'],
            'weekly_seo_audit'       => ['provider_cost' => 0.0800, 'markup' => 'tiny', 'unit' => 'audit'],
            'seo_audit'              => ['provider_cost' => 0.0800, 'markup' => 'tiny', 'unit' => 'audit'],
            'monthly_blog'           => ['provider_cost' => 0.0500, 'markup' => 'tiny', 'unit' => 'post'],
            'automated_blog'         => ['provider_cost' => 0.0500, 'markup' => 'tiny', 'unit' => 'post'],
            'competitor_monitoring'  => ['provider_cost' => 0.0500, 'markup' => 'tiny', 'unit' => 'scan'],
            'competitor_analysis'    => ['provider_cost' => 0.0500, 'markup' => 'tiny', 'unit' => 'scan'],
            'email_strategy_review'  => ['provider_cost' => 0.0400, 'markup' => 'tiny', 'unit' => 'review'],
            'email_strategy'         => ['provider_cost' => 0.0400, 'markup' => 'tiny', 'unit' => 'review'],
            'content_embeddings'     => ['provider_cost' => 0.0100, 'markup' => 'tiny', 'unit' => 'batch'],
            'embeddings'             => ['provider_cost' => 0.0100, 'markup' => 'tiny', 'unit' => 'batch'],
            'call_summary'           => ['provider_cost' => 0.0150, 'markup' => 'tiny', 'unit' => 'summary'],
            'call_transcription'     => ['provider_cost' => 0.0150, 'markup' => 'tiny', 'unit' => 'summary'],

            // ─── Quarterly / tax ──────────────────────────────────────────
            'quarterly_insights'     => ['provider_cost' => 0.0600, 'markup' => 'standard', 'unit' => 'report'],
            'quarterly_report'       => ['provider_cost' => 0.0600, 'markup' => 'standard', 'unit' => 'report'],
            'tax_calculation'        => ['provider_cost' => 0.0800, 'markup' => 'standard', 'unit' => 'calc'],
            'tax_estimate'           => ['provider_cost' => 0.0800, 'markup' => 'standard', 'unit' => 'calc'],
            'roadmap'                => ['provider_cost' => 0.1500, 'markup' => 'standard', 'unit' => 'roadmap'],
            'roadmap_generation'     => ['provider_cost' => 0.1500, 'markup' => 'standard', 'unit' => 'roadmap'],
            'media_integration'      => ['provider_cost' => 0.0600, 'markup' => 'standard', 'unit' => 'integration'],

            // ─── Realtime voice (premium AI minutes) ──────────────────────
            // OpenAI Realtime audio ~$0.06 input + $0.24 output per minute = ~$0.086 / min raw
            // 12× standard markup → $1.032 / min user charge
            'realtime_premium_ai_min'=> ['provider_cost' => 0.0860, 'markup' => 'standard', 'unit' => 'minute', 'notes' => 'OpenAI Realtime conversational audio'],
            'realtime_audio_min'     => ['provider_cost' => 0.0860, 'markup' => 'standard', 'unit' => 'minute'],
            'realtime_audio_sdp'     => ['provider_cost' => 0.0860, 'markup' => 'standard', 'unit' => 'minute', 'notes' => 'Backward-compatible alias for Premium Live Voice first-minute SDP exchange'],

            // ─── Phone / SMS / carrier (2.5× passthrough) ─────────────────
            'call_outbound'          => ['provider_cost' => 0.0140, 'markup' => 'phone', 'unit' => 'minute'],
            'call_outbound_min'      => ['provider_cost' => 0.0140, 'markup' => 'phone', 'unit' => 'minute'],
            'call_inbound'           => ['provider_cost' => 0.0085, 'markup' => 'phone', 'unit' => 'minute'],
            'call_inbound_min'       => ['provider_cost' => 0.0085, 'markup' => 'phone', 'unit' => 'minute'],
            'sms_outbound'           => ['provider_cost' => 0.0079, 'markup' => 'phone', 'unit' => 'message'],
            'sms_inbound'            => ['provider_cost' => 0.0075, 'markup' => 'phone', 'unit' => 'message'],
            'a2p_surcharge'          => ['provider_cost' => 0.0050, 'markup' => 'phone', 'unit' => 'message'],
            'recording'              => ['provider_cost' => 0.0025, 'markup' => 'phone', 'unit' => 'minute'],
            'call_recording'         => ['provider_cost' => 0.0025, 'markup' => 'phone', 'unit' => 'minute'],
            'phone_activation'       => ['provider_cost' => 1.0000, 'markup' => 'phone', 'unit' => 'activation'],
            'phone_rental'           => ['provider_cost' => 1.1500, 'markup' => 'phone', 'unit' => 'month'],
            'phone_monthly'          => ['provider_cost' => 1.1500, 'markup' => 'phone', 'unit' => 'month'],
            'a2p_brand'              => ['provider_cost' => 4.0000, 'markup' => 'phone', 'unit' => 'registration'],
            'a2p_campaign'           => ['provider_cost' => 15.0000, 'markup' => 'phone', 'unit' => 'month'],
            'a2p_campaign_monthly'   => ['provider_cost' => 15.0000, 'markup' => 'phone', 'unit' => 'month'],
            'extra_call_leg'         => ['provider_cost' => 0.0140, 'markup' => 'phone', 'unit' => 'leg'],

            // ─── Phone-side AI (TTS, GPT turn) ────────────────────────────
            // These are AI operations connected to phone, charged at standard 12× AI markup
            'phone_tts'              => ['provider_cost' => 0.0150, 'markup' => 'standard', 'unit' => '1k_chars'],
            'phone_tts_1k'           => ['provider_cost' => 0.0150, 'markup' => 'standard', 'unit' => '1k_chars'],
            'phone_ai_turn'          => ['provider_cost' => 0.0030, 'markup' => 'tiny',     'unit' => 'turn'],
            'phone_gpt_turn'         => ['provider_cost' => 0.0030, 'markup' => 'tiny',     'unit' => 'turn'],

            // ─── Email (rationing and outbound-provider control) ─────────────
            'email'                  => ['provider_cost' => 0.0010, 'markup' => 'tiny', 'unit' => 'email'],
        ];

        return apply_filters('vmo_operation_catalog', $catalog);
    }
}

if (!function_exists('vmo_catalog_get_user_charge_usd')) {
    /**
     * User-facing dollar charge for a given operation key.
     */
    function vmo_catalog_get_user_charge_usd($operation_key) {
        $catalog = vmo_operation_catalog();
        if (!isset($catalog[$operation_key])) {
            return null; // unknown operation
        }
        $entry = $catalog[$operation_key];
        return vmo_apply_markup($entry['provider_cost'], $entry['markup']);
    }
}

if (!function_exists('vmo_catalog_get_token_cost')) {
    /**
     * Token cost for a given operation key, derived from dollar charge.
     */
    function vmo_catalog_get_token_cost($operation_key) {
        $usd = vmo_catalog_get_user_charge_usd($operation_key);
        if ($usd === null) return null;
        return vmo_dollars_to_tokens($usd);
    }
}

if (!function_exists('vmo_catalog_has_operation')) {
    function vmo_catalog_has_operation($operation_key) {
        $catalog = vmo_operation_catalog();
        return isset($catalog[$operation_key]);
    }
}

if (!function_exists('vmo_catalog_get_provider_cost')) {
    /**
     * Raw provider cost in USD for an operation (for ledger / margin reporting).
     */
    function vmo_catalog_get_provider_cost($operation_key) {
        $catalog = vmo_operation_catalog();
        if (!isset($catalog[$operation_key])) return null;
        return (float) $catalog[$operation_key]['provider_cost'];
    }
}
