<?php

declare(strict_types=1);

return [
    'queue' => env('DISCOVERY_QUEUE', env('RECOMMENDATIONS_QUEUE', env('VISION_QUEUE', 'default'))),

    // Versioned from day one for safe future migrations/experiments.
    'profile_version' => env('DISCOVERY_PROFILE_VERSION', 'profile-v1'),
    'event_version' => env('DISCOVERY_EVENT_VERSION', 'event-v1'),
    'algo_version' => env('DISCOVERY_ALGO_VERSION', env('RECOMMENDATIONS_ALGO_VERSION', 'clip-cosine-v1')),
    'cache_version' => env('DISCOVERY_CACHE_VERSION', 'cache-v1'),

    'decay' => [
        // Exponential half-life: score * 0.5 every N hours.
        'half_life_hours' => (float) env('DISCOVERY_DECAY_HALF_LIFE_HOURS', 72),
    ],

    // Baseline event contribution weights.
    'weights' => [
        'view' => (float) env('DISCOVERY_WEIGHT_VIEW', 1.0),
        'click' => (float) env('DISCOVERY_WEIGHT_CLICK', 2.0),
        'favorite' => (float) env('DISCOVERY_WEIGHT_FAVORITE', 4.0),
        'download' => (float) env('DISCOVERY_WEIGHT_DOWNLOAD', 3.0),
        'dwell' => (float) env('DISCOVERY_WEIGHT_DWELL', 1.5),
        'scroll' => (float) env('DISCOVERY_WEIGHT_SCROLL', 0.75),
    ],

    // Recommendation cache TTL in minutes (schema foundation only for now).
    'cache_ttl_minutes' => (int) env('DISCOVERY_CACHE_TTL_MINUTES', 60),

    'v2' => [
        'enabled' => (bool) env('DISCOVERY_V2_ENABLED', false),
        'algo_version' => env('DISCOVERY_V2_ALGO_VERSION', 'clip-cosine-v2-adaptive'),
        'cache_version' => env('DISCOVERY_V2_CACHE_VERSION', 'cache-v2'),
        'cache_ttl_minutes' => (int) env('DISCOVERY_V2_CACHE_TTL_MINUTES', 15),
        'candidate_pool_max' => (int) env('DISCOVERY_V2_CANDIDATE_POOL_MAX', 300),
        'feed_pool_size' => (int) env('DISCOVERY_V2_FEED_POOL_SIZE', 240),
        'fresh_upload_hours' => (int) env('DISCOVERY_V2_FRESH_UPLOAD_HOURS', 72),
        'new_creator_days' => (int) env('DISCOVERY_V2_NEW_CREATOR_DAYS', 45),
        'max_per_creator' => (int) env('DISCOVERY_V2_MAX_PER_CREATOR', 3),
        'repetition_window' => (int) env('DISCOVERY_V2_REPETITION_WINDOW', 24),
        'rollout_percentage' => (int) env('DISCOVERY_V2_ROLLOUT_PERCENTAGE', 0),

        'layers' => [
            'personalized' => (float) env('DISCOVERY_V2_LAYER_PERSONALIZED', 0.50),
            'social' => (float) env('DISCOVERY_V2_LAYER_SOCIAL', 0.20),
            'trending' => (float) env('DISCOVERY_V2_LAYER_TRENDING', 0.20),
            'exploration' => (float) env('DISCOVERY_V2_LAYER_EXPLORATION', 0.10),
        ],

        'session' => [
            'ttl_seconds' => (int) env('DISCOVERY_V2_SESSION_TTL_SECONDS', 14400),
            'max_items' => (int) env('DISCOVERY_V2_SESSION_MAX_ITEMS', 120),
            'half_life_hours' => (float) env('DISCOVERY_V2_SESSION_HALF_LIFE_HOURS', 8),
            'merge_multiplier' => (float) env('DISCOVERY_V2_SESSION_MERGE_MULTIPLIER', 1.35),
            'artwork_similarity_weight' => (float) env('DISCOVERY_V2_SESSION_ARTWORK_SIMILARITY_WEIGHT', 0.85),
            'event_weights' => [
                'view' => (float) env('DISCOVERY_V2_SESSION_WEIGHT_VIEW', 1.0),
                'click' => (float) env('DISCOVERY_V2_SESSION_WEIGHT_CLICK', 2.0),
                'favorite' => (float) env('DISCOVERY_V2_SESSION_WEIGHT_FAVORITE', 4.5),
                'download' => (float) env('DISCOVERY_V2_SESSION_WEIGHT_DOWNLOAD', 3.5),
                'dwell' => (float) env('DISCOVERY_V2_SESSION_WEIGHT_DWELL', 1.75),
                'scroll' => (float) env('DISCOVERY_V2_SESSION_WEIGHT_SCROLL', 0.75),
            ],
        ],

        'weights' => [
            'base' => (float) env('DISCOVERY_V2_WEIGHT_BASE', 1.0),
            'session' => (float) env('DISCOVERY_V2_WEIGHT_SESSION', 1.4),
            'social' => (float) env('DISCOVERY_V2_WEIGHT_SOCIAL', 1.1),
            'trending' => (float) env('DISCOVERY_V2_WEIGHT_TRENDING', 0.95),
            'exploration' => (float) env('DISCOVERY_V2_WEIGHT_EXPLORATION', 0.7),
            'creator' => (float) env('DISCOVERY_V2_WEIGHT_CREATOR', 0.5),
            'repetition_penalty' => (float) env('DISCOVERY_V2_WEIGHT_REPETITION_PENALTY', 0.45),
            'negative_tag_penalty' => (float) env('DISCOVERY_V2_WEIGHT_NEGATIVE_TAG_PENALTY', 0.65),
            'followed_creator' => (float) env('DISCOVERY_V2_WEIGHT_FOLLOWED_CREATOR', 0.85),
            'followed_like' => (float) env('DISCOVERY_V2_WEIGHT_FOLLOWED_LIKE', 0.55),
        ],

        'trending' => [
            'period_weights' => [
                '1h' => (float) env('DISCOVERY_V2_TRENDING_WEIGHT_1H', 1.0),
                '24h' => (float) env('DISCOVERY_V2_TRENDING_WEIGHT_24H', 0.7),
                '7d' => (float) env('DISCOVERY_V2_TRENDING_WEIGHT_7D', 0.45),
            ],
            'velocity_weights' => [
                'views' => (float) env('DISCOVERY_V2_TRENDING_VIEWS_WEIGHT', 1.0),
                'favorites' => (float) env('DISCOVERY_V2_TRENDING_FAVORITES_WEIGHT', 3.0),
                'comments' => (float) env('DISCOVERY_V2_TRENDING_COMMENTS_WEIGHT', 2.5),
                'shares' => (float) env('DISCOVERY_V2_TRENDING_SHARES_WEIGHT', 2.0),
            ],
            'age_decay_per_hour' => (float) env('DISCOVERY_V2_TRENDING_AGE_DECAY_PER_HOUR', 0.08),
        ],

        'exploration' => [
            'creator_bonus' => (float) env('DISCOVERY_V2_EXPLORATION_CREATOR_BONUS', 0.6),
            'tag_bonus' => (float) env('DISCOVERY_V2_EXPLORATION_TAG_BONUS', 0.45),
            'freshness_bonus' => (float) env('DISCOVERY_V2_EXPLORATION_FRESHNESS_BONUS', 0.55),
        ],

        'negative_signals' => [
            'hide_artwork_penalty' => (float) env('DISCOVERY_V2_HIDE_ARTWORK_PENALTY', 5.0),
            'dislike_tag_penalty' => (float) env('DISCOVERY_V2_DISLIKE_TAG_PENALTY', 0.75),
        ],
    ],

    'v3' => [
        'enabled' => (bool) env('DISCOVERY_V3_ENABLED', false),
        'cache_version' => env('DISCOVERY_V3_CACHE_VERSION', 'cache-v3'),
        'cache_ttl_minutes' => (int) env('DISCOVERY_V3_CACHE_TTL_MINUTES', 5),
        'vector_similarity_weight' => (float) env('DISCOVERY_V3_VECTOR_SIMILARITY_WEIGHT', 0.8),
        'vector_base_score' => (float) env('DISCOVERY_V3_VECTOR_BASE_SCORE', 0.75),
        'max_seed_artworks' => (int) env('DISCOVERY_V3_MAX_SEED_ARTWORKS', 3),
        'vector_candidate_pool' => (int) env('DISCOVERY_V3_VECTOR_CANDIDATE_POOL', 60),
        'sections' => [
            'similar_style_limit' => (int) env('DISCOVERY_V3_SECTION_SIMILAR_STYLE_LIMIT', 3),
            'you_may_also_like_limit' => (int) env('DISCOVERY_V3_SECTION_YOU_MAY_ALSO_LIKE_LIMIT', 6),
            'visually_related_limit' => (int) env('DISCOVERY_V3_SECTION_VISUALLY_RELATED_LIMIT', 6),
        ],
    ],

    // Phase 8B: versioned ranking blend weights.
    // Blend components: w1=interest, w2=recency, w3=popularity, w4=novelty.
    'ranking' => [
        'default_weights' => [
            'version' => env('DISCOVERY_RANKING_WEIGHTS_VERSION', 'rank-w-v1'),
            'w1' => (float) env('DISCOVERY_RANKING_W1', 0.65),
            'w2' => (float) env('DISCOVERY_RANKING_W2', 0.20),
            'w3' => (float) env('DISCOVERY_RANKING_W3', 0.10),
            'w4' => (float) env('DISCOVERY_RANKING_W4', 0.05),
        ],

        // Per-algo overrides for safe rollout by algo_version.
        'algo_weight_sets' => [
            'clip-cosine-v1' => [
                'version' => env('DISCOVERY_RANKING_WEIGHTS_VERSION_CLIP_COSINE_V1', 'rank-w-v1'),
                'w1' => (float) env('DISCOVERY_RANKING_W1_CLIP_COSINE_V1', 0.65),
                'w2' => (float) env('DISCOVERY_RANKING_W2_CLIP_COSINE_V1', 0.20),
                'w3' => (float) env('DISCOVERY_RANKING_W3_CLIP_COSINE_V1', 0.10),
                'w4' => (float) env('DISCOVERY_RANKING_W4_CLIP_COSINE_V1', 0.05),
            ],
            'clip-cosine-v2' => [
                'version' => env('DISCOVERY_RANKING_WEIGHTS_VERSION_CLIP_COSINE_V2', 'rank-w-v2-prod-1'),
                'w1' => (float) env('DISCOVERY_RANKING_W1_CLIP_COSINE_V2', 0.52),
                'w2' => (float) env('DISCOVERY_RANKING_W2_CLIP_COSINE_V2', 0.23),
                'w3' => (float) env('DISCOVERY_RANKING_W3_CLIP_COSINE_V2', 0.15),
                'w4' => (float) env('DISCOVERY_RANKING_W4_CLIP_COSINE_V2', 0.10),
            ],
        ],
    ],

    // Phase 8 production rollout gates (deterministic user bucketing).
    'rollout' => [
        'enabled' => (bool) env('DISCOVERY_ROLLOUT_ENABLED', false),
        'baseline_algo_version' => env('DISCOVERY_ROLLOUT_BASELINE_ALGO_VERSION', 'clip-cosine-v1'),
        'candidate_algo_version' => env('DISCOVERY_ROLLOUT_CANDIDATE_ALGO_VERSION', 'clip-cosine-v2'),

        // One of: g10, g50, g100.
        'active_gate' => env('DISCOVERY_ROLLOUT_ACTIVE_GATE', 'g10'),
        'gates' => [
            'g10' => [
                'name' => '10%',
                'percentage' => (int) env('DISCOVERY_ROLLOUT_GATE_10_PERCENT', 10),
            ],
            'g50' => [
                'name' => '50%',
                'percentage' => (int) env('DISCOVERY_ROLLOUT_GATE_50_PERCENT', 50),
            ],
            'g100' => [
                'name' => '100%',
                'percentage' => (int) env('DISCOVERY_ROLLOUT_GATE_100_PERCENT', 100),
            ],
        ],

        // Emergency rollback toggle: force all traffic to one algo_version.
        'force_algo_version' => env('DISCOVERY_FORCE_ALGO_VERSION', ''),

        // Guardrails (used operationally in runbook and dashboards).
        'monitoring_thresholds' => [
            'ctr_warn_drop_pct' => (float) env('DISCOVERY_ROLLOUT_WARN_CTR_DROP_PCT', 3.0),
            'ctr_rollback_drop_pct' => (float) env('DISCOVERY_ROLLOUT_ROLLBACK_CTR_DROP_PCT', 5.0),
            'long_dwell_warn_drop_pct' => (float) env('DISCOVERY_ROLLOUT_WARN_LONG_DWELL_DROP_PCT', 4.0),
            'long_dwell_rollback_drop_pct' => (float) env('DISCOVERY_ROLLOUT_ROLLBACK_LONG_DWELL_DROP_PCT', 8.0),
            'diversity_warn_concentration_rise_pct' => (float) env('DISCOVERY_ROLLOUT_WARN_DIVERSITY_CONCENTRATION_RISE_PCT', 10.0),
            'diversity_rollback_concentration_rise_pct' => (float) env('DISCOVERY_ROLLOUT_ROLLBACK_DIVERSITY_CONCENTRATION_RISE_PCT', 15.0),
        ],
    ],

    // Offline evaluation objective weights (manual/data-driven tuning).
    'evaluation' => [
        'objective_weights' => [
            'ctr' => (float) env('DISCOVERY_EVAL_WEIGHT_CTR', 0.45),
            'save_rate' => (float) env('DISCOVERY_EVAL_WEIGHT_SAVE_RATE', 0.35),
            'long_dwell_share' => (float) env('DISCOVERY_EVAL_WEIGHT_LONG_DWELL', 0.25),
            'bounce_rate_penalty' => (float) env('DISCOVERY_EVAL_WEIGHT_BOUNCE_PENALTY', 0.15),
        ],
        // Temporary switch: keep save_rate visible but exclude it from objective score.
        'save_rate_informational' => (bool) env('DISCOVERY_EVAL_SAVE_RATE_INFORMATIONAL', true),
    ],
];
