<?php

namespace App\Library\CampaignDecision;

class CampaignUnifiedVerdictCombiner
{
    /**
     * @param  array<string, mixed>  $channels
     * @param  array<string, float|int>  $settings
     * @return array{
     *     verdict: CampaignDecisionVerdict,
     *     reason: string,
     *     reasons: list<array<string, mixed>>,
     *     recommended_action: string,
     *     recommended_budget_change_percent: int
     * }
     */
    public function combine(
        CampaignPerformanceContext $context,
        array $channels,
        array $settings,
        array $dataQuality,
    ): array {
        $reasons = [];

        if (($dataQuality['status'] ?? '') === 'BROKEN') {
            foreach ($dataQuality['reasons'] as $row) {
                $reasons[] = [...$row, 'channel' => 'platform'];
            }

            return $this->result(
                CampaignDecisionVerdict::DataIssue,
                'Fix platform ingest or mapping before changing the campaign.',
                $reasons,
                'NONE',
                0,
            );
        }

        $learningStage = $this->metaLearningStage($context);
        if ($learningStage === 'LEARNING' && $this->learningLimitedBySpendCap($context)) {
            $learningStage = 'LEARNING_LIMITED';
        }
        $platformStatus = strtoupper(trim((string) ($context->platformStatus ?? '')));
        $platformName = match (strtolower($context->platform)) {
            'google' => 'Google',
            'linkedin' => 'LinkedIn',
            default => 'Meta',
        };

        if (in_array($platformStatus, ['ARCHIVED', 'DELETED', 'REMOVED'], true)) {
            $reasons[] = [
                'metric' => 'platform_status',
                'channel' => 'platform',
                'current' => $platformStatus,
                'impact' => 'negative',
            ];

            return $this->result(
                CampaignDecisionVerdict::Watch,
                'This campaign is archived on '.$platformName.'. Review the live copy, not this row.',
                $reasons,
                'NONE',
                0,
            );
        }

        if (in_array($platformStatus, ['PAUSED', 'CAMPAIGN_PAUSED'], true)) {
            $reasons[] = [
                'metric' => 'platform_status',
                'channel' => 'platform',
                'current' => $platformStatus,
                'impact' => 'neutral',
            ];

            return $this->result(
                CampaignDecisionVerdict::Maintain,
                'Campaign is paused on '.$platformName.'. Resume it before judging delivery or budget.',
                $reasons,
                'NO_CHANGE',
                0,
            );
        }

        if ($this->hourCriticalPause($context, $channels, $settings)) {
            $reasons[] = [
                'metric' => 'hour_spend_or_cpc',
                'channel' => 'hour',
                'impact' => 'negative',
            ];

            return $this->result(
                CampaignDecisionVerdict::Pause,
                'Recent hours show wasteful spend (no clicks or CPC above limit).',
                $reasons,
                'PAUSE',
                0,
            );
        }

        $notDelivering = $this->isNotDelivering($context, $channels, $settings, $learningStage);

        if ($notDelivering) {
            $reasons[] = [
                'metric' => 'delivery',
                'channel' => 'platform',
                'impact' => 'negative',
                'current' => (float) ($context->windows['7d']['spend'] ?? 0),
                'learning_stage' => $learningStage,
            ];

            return $this->result(
                CampaignDecisionVerdict::Watch,
                'Not delivering — bid or audience is blocking spend. Prefer lowest cost (remove a cost cap) before raising budget or testing creatives.',
                $reasons,
                'NONE',
                0,
            );
        }

        if ($learningStage === 'SUCCESS' && $this->hasHealthyDelivery($context, $channels)) {
            $reasons[] = [
                'metric' => 'learning_stage',
                'channel' => 'platform',
                'impact' => 'positive',
                'current' => $learningStage,
            ];

            return $this->result(
                CampaignDecisionVerdict::Maintain,
                'Working — '.$platformName.' learning succeeded and delivery is healthy. Keep the setup; do not restart learning with bid, audience, or placement changes.',
                $reasons,
                'NO_CHANGE',
                0,
            );
        }

        if (in_array($learningStage, ['FAIL', 'LEARNING_LIMITED'], true) && $this->pastLearningFloor($context, $settings)) {
            $reasons[] = [
                'metric' => 'learning_stage',
                'channel' => 'platform',
                'impact' => 'negative',
                'current' => $learningStage,
                'age_days' => $context->campaignAgeDays,
            ];

            return $this->result(
                CampaignDecisionVerdict::Watch,
                $platformName.' learning is '.$learningStage.'. This is not passive monitoring — a cost cap or tiny spend versus the daily budget is blocking delivery. Switch to lowest cost or raise the cap before waiting.',
                $reasons,
                'NONE',
                0,
            );
        }

        if ($this->stuckInLearningWithoutResults($context, $channels, $settings, $learningStage)) {
            $spend = $this->evaluationSpend($context);
            $age = $context->campaignAgeDays;
            $reasons[] = [
                'metric' => 'learning_stuck',
                'channel' => 'platform',
                'impact' => 'negative',
                'current' => $learningStage,
                'age_days' => $age,
                'spend' => $spend,
            ];

            return $this->result(
                CampaignDecisionVerdict::Watch,
                $platformName.' is still in learning because this campaign has run '.$age.' days with $'.number_format($spend, 2).' and 0 conversions. That is a conversion problem, not a learning period.',
                $reasons,
                'NONE',
                0,
            );
        }

        if ($this->isLearning($context, $settings, $learningStage)) {
            $reasons[] = [
                'metric' => 'learning_period',
                'channel' => 'platform',
                'impact' => 'neutral',
                'current' => $context->campaignAgeDays,
                'target' => (int) ($settings['learning_period_days'] ?? 3),
                'learning_stage' => $learningStage,
                'latch' => $this->learningLatch($context, $settings, $learningStage),
            ];

            return $this->result(
                CampaignDecisionVerdict::Learning,
                'Campaign is still in '.$platformName.' learning ('.$this->learningLatch($context, $settings, $learningStage).'). Keep changes light until there is enough delivery to judge.',
                $reasons,
                'NONE',
                0,
            );
        }

        $outcome = $channels['outcome'] ?? null;

        if (is_array($outcome) && ($outcome['available'] ?? false)) {
            $outcomeVerdict = $this->evaluateOutcome($outcome, $settings, $reasons);

            if ($outcomeVerdict !== null) {
                return $outcomeVerdict;
            }
        }

        if (! $this->hasEvaluationVolume($channels, $settings)) {
            return $this->result(
                CampaignDecisionVerdict::InsufficientData,
                'Not enough spend or clicks yet for a revenue-based decision.',
                $reasons,
                'NONE',
                0,
            );
        }

        if (! $this->isRetargeting($context) && $this->hourChannelAlarming($channels, $settings)) {
            $reasons[] = ['metric' => 'hour_channel', 'channel' => 'hour', 'impact' => 'negative'];

            return $this->result(
                CampaignDecisionVerdict::Watch,
                'Platform hour metrics are weak; wait for lagged outcome data to confirm.',
                $reasons,
                'NONE',
                0,
            );
        }

        return $this->result(
            CampaignDecisionVerdict::Maintain,
            'Metrics are within acceptable limits on available windows.',
            $reasons,
            'NO_CHANGE',
            0,
        );
    }

    private function metaLearningStage(CampaignPerformanceContext $context): ?string
    {
        $raw = strtoupper(trim((string) ($context->deliveryStatus ?? '')));

        return match (true) {
            $raw === '' => null,
            in_array($raw, ['SUCCESS', 'LEARNING_SUCCESS'], true) => 'SUCCESS',
            in_array($raw, ['FAIL', 'LEARNING_FAIL'], true) => 'FAIL',
            in_array($raw, ['LEARNING_LIMITED', 'LIMITED'], true) => 'LEARNING_LIMITED',
            in_array($raw, ['LEARNING'], true) => 'LEARNING',
            default => $raw,
        };
    }

    /**
     * @param  array<string, mixed>  $channels
     * @param  array<string, float|int>  $settings
     */
    private function isNotDelivering(
        CampaignPerformanceContext $context,
        array $channels,
        array $settings,
        ?string $learningStage,
    ): bool {
        $hourSpend = (float) data_get($channels, 'hour.spend', 0);

        if ($hourSpend > 0) {
            return false;
        }

        $spend = (float) ($context->windows['7d']['spend'] ?? data_get($channels, 'day.spend', 0));
        $impressions = (int) ($context->windows['7d']['impressions'] ?? data_get($channels, 'day.impressions', 0));
        $maxSpend = (float) ($settings['not_delivering_max_spend_7d'] ?? 5);
        $maxImpressions = (int) ($settings['not_delivering_max_impressions_7d'] ?? 100);

        if ($learningStage === 'FAIL' && $spend <= $maxSpend && $impressions <= $maxImpressions) {
            return true;
        }

        return $context->campaignAgeDays >= (int) ($settings['learning_period_days'] ?? 3)
            && $spend <= 0
            && $impressions <= 0;
    }

    /**
     * @param  array<string, mixed>  $channels
     */
    private function hasHealthyDelivery(CampaignPerformanceContext $context, array $channels): bool
    {
        $spend = (float) ($context->windows['7d']['spend'] ?? data_get($channels, 'day.spend', 0));
        $clicks = (int) ($context->windows['7d']['clicks'] ?? data_get($channels, 'day.clicks', 0));
        $impressions = (int) ($context->windows['7d']['impressions'] ?? data_get($channels, 'day.impressions', 0));

        return $spend >= 10 && ($clicks >= 20 || $impressions >= 500);
    }

    /**
     * @param  array<string, float|int>  $settings
     */
    private function learningLimitedBySpendCap(CampaignPerformanceContext $context): bool
    {
        $budget = $context->dailyBudget;
        $spend = (float) ($context->windows['7d']['spend'] ?? 0);
        $expected = is_numeric($budget) ? ((float) $budget) * 7 * 0.4 : 0.0;

        return $context->campaignAgeDays >= 7
            && $expected > 0
            && $spend < $expected;
    }

    private function pastLearningFloor(CampaignPerformanceContext $context, array $settings): bool
    {
        $learningDays = (int) ($settings['learning_period_days'] ?? 3);
        $maxTestSpend = (float) ($settings['max_test_spend'] ?? 40);
        $spend = (float) ($context->windows['7d']['spend'] ?? 0);

        if (($context->affiliate['revenue_source'] ?? null) === 'roi_dashboard') {
            $spend = max($spend, (float) data_get($context->affiliate, 'outcome_window.spend', 0));
        }

        return $context->campaignAgeDays >= $learningDays || $spend >= $maxTestSpend;
    }

    /**
     * @param  array<string, float|int>  $settings
     */
    private function learningLatch(CampaignPerformanceContext $context, array $settings, ?string $learningStage): string
    {
        $learningDays = (int) ($settings['learning_period_days'] ?? 3);
        $maxTestSpend = (float) ($settings['max_test_spend'] ?? 40);
        $spend = (float) ($context->windows['7d']['spend'] ?? 0);

        if ($context->campaignAgeDays < $learningDays) {
            return 'age '.$context->campaignAgeDays.'d of '.$learningDays.'d';
        }

        if ($spend < $maxTestSpend) {
            return 'spend $'.number_format($spend, 2).' under $'.number_format($maxTestSpend, 2).' test floor';
        }

        if ($learningStage === 'LEARNING') {
            $platformName = match (strtolower($context->platform)) {
                'google' => 'Google',
                'linkedin' => 'LinkedIn',
                default => 'Meta',
            };

            return $platformName.' status LEARNING';
        }

        return 'learning window';
    }

    /**
     * @param  array<string, float|int>  $settings
     */
    private function isLearning(CampaignPerformanceContext $context, array $settings, ?string $learningStage = null): bool
    {
        if (in_array($learningStage, ['SUCCESS', 'FAIL', 'LEARNING_LIMITED'], true)) {
            return false;
        }

        $learningDays = (int) ($settings['learning_period_days'] ?? 3);
        $maxTestSpend = (float) ($settings['max_test_spend'] ?? 40);
        $lifetimeSpend = (float) ($context->windows['7d']['spend'] ?? 0);

        if (($context->affiliate['revenue_source'] ?? null) === 'roi_dashboard') {
            $lifetimeSpend = max($lifetimeSpend, (float) data_get($context->affiliate, 'outcome_window.spend', 0));
        }

        if ($context->campaignAgeDays < $learningDays) {
            return true;
        }

        if ($lifetimeSpend < $maxTestSpend) {
            return true;
        }

        return $learningStage === 'LEARNING';
    }

    /**
     * @param  array<string, mixed>  $channels
     * @param  array<string, float|int>  $settings
     */
    private function stuckInLearningWithoutResults(
        CampaignPerformanceContext $context,
        array $channels,
        array $settings,
        ?string $learningStage,
    ): bool {
        if ($learningStage !== 'LEARNING') {
            return false;
        }

        $stuckDays = (int) ($settings['learning_stuck_days'] ?? 7);
        $maxTestSpend = (float) ($settings['max_test_spend'] ?? 40);

        if ($context->campaignAgeDays < $stuckDays) {
            return false;
        }

        if ($this->evaluationSpend($context) < $maxTestSpend) {
            return false;
        }

        return $this->conversionCount($context, $channels) <= 0;
    }

    /**
     * @param  array<string, mixed>  $channels
     */
    private function conversionCount(CampaignPerformanceContext $context, array $channels): float
    {
        $windows = $context->windows;
        $thirty = is_array($windows['30d'] ?? null) ? $windows['30d'] : [];
        $seven = is_array($windows['7d'] ?? null) ? $windows['7d'] : [];
        $day = is_array($channels['day'] ?? null) ? $channels['day'] : [];

        return (float) ($thirty['conversions'] ?? 0)
            + (float) ($thirty['leads'] ?? 0)
            + (float) ($seven['conversions'] ?? 0)
            + (float) ($seven['leads'] ?? 0)
            + (float) ($day['conversions'] ?? 0)
            + (float) ($day['leads'] ?? 0)
            + (float) data_get($context->affiliate, 'outcome_window.orders', 0)
            + (float) data_get($context->affiliate, 'outcome_window.leads', 0);
    }

    private function evaluationSpend(CampaignPerformanceContext $context): float
    {
        $thirty = (float) data_get($context->windows, '30d.spend', 0);
        $seven = (float) data_get($context->windows, '7d.spend', 0);

        return max($thirty, $seven);
    }

    /**
     * @param  array<string, mixed>  $channels
     * @param  array<string, float|int>  $settings
     */
    private function hourCriticalPause(CampaignPerformanceContext $context, array $channels, array $settings): bool
    {
        $hour = $channels['hour'] ?? [];
        $floor = (float) ($settings['zero_click_spend_floor'] ?? 10);
        $maxCpc = (float) ($settings['max_cpc'] ?? 2);
        $spend = (float) ($hour['spend'] ?? 0);
        $clicks = (int) ($hour['clicks'] ?? 0);
        $cpc = $hour['cpc'] ?? null;

        if ($spend >= $floor && $clicks === 0) {
            return true;
        }

        if ($this->isRetargeting($context)) {
            return false;
        }

        return $clicks > 0 && $cpc !== null && $cpc > $maxCpc && $spend >= $floor;
    }

    private function isRetargeting(CampaignPerformanceContext $context): bool
    {
        $name = strtolower($context->name);

        return str_contains($name, 'retarget')
            || str_contains($name, 'remarket')
            || str_contains($name, 'lp visitor');
    }

    /**
     * @param  array<string, mixed>  $channels
     * @param  array<string, float|int>  $settings
     */
    private function hourChannelAlarming(array $channels, array $settings): bool
    {
        $hour = $channels['hour'] ?? [];
        $maxCpc = (float) ($settings['max_cpc'] ?? 2);

        return isset($hour['cpc']) && $hour['cpc'] > $maxCpc * 0.9;
    }

    /**
     * @param  array<string, mixed>  $channels
     * @param  array<string, float|int>  $settings
     */
    private function hasEvaluationVolume(array $channels, array $settings): bool
    {
        $day = $channels['day'] ?? [];
        $minSpend = (float) ($settings['minimum_spend'] ?? 25);
        $minClicks = (int) ($settings['minimum_clicks'] ?? 20);

        return (float) ($day['spend'] ?? 0) >= $minSpend
            || (int) ($day['clicks'] ?? 0) >= $minClicks;
    }

    /**
     * @param  array<string, mixed>  $outcome
     * @param  array<string, float|int>  $settings
     * @param  list<array<string, mixed>>  $reasons
     * @return array{
     *     verdict: CampaignDecisionVerdict,
     *     reason: string,
     *     reasons: list<array<string, mixed>>,
     *     recommended_action: string,
     *     recommended_budget_change_percent: int
     * }|null
     */
    private function evaluateOutcome(array $outcome, array $settings, array &$reasons): ?array
    {
        $roas = $outcome['roas'] ?? null;
        $cpa = $outcome['cpa'] ?? null;
        $orders = (float) ($outcome['orders'] ?? 0);
        $revenue = (float) ($outcome['revenue'] ?? 0);
        $spend = (float) ($outcome['platform_spend'] ?? 0);
        $roi = ($outcome['revenue_source'] ?? null) === 'roi_dashboard';
        $minRoas = (float) ($settings['minimum_roas'] ?? 1.2);
        $targetRoas = (float) ($settings['target_roas'] ?? 2.0);
        $maxCpa = (float) ($settings['maximum_cpa'] ?? 75);
        $targetCpa = (float) ($settings['target_cpa'] ?? 50);
        $minOrders = (float) ($settings['minimum_orders'] ?? 1);
        $minSpend = (float) ($settings['minimum_spend'] ?? 25);

        if ($roi && $revenue > 0) {
            $orders = max($orders, $minOrders);
        }

        if ($roi && $revenue <= 0 && $spend >= $minSpend) {
            $countVerdict = $this->evaluateResultCount($outcome, $spend, $settings, $reasons);

            if ($countVerdict !== null) {
                return $countVerdict;
            }
        }

        if ($spend >= $minSpend && $orders < $minOrders) {
            $reasons[] = [
                'metric' => 'orders',
                'channel' => 'outcome',
                'current' => $orders,
                'target' => $minOrders,
                'impact' => 'negative',
            ];

            return $this->result(
                CampaignDecisionVerdict::Pause,
                $roi
                    ? 'ROI spend in the lagged window has no sales.'
                    : 'Mature window has spend but no affiliate orders.',
                $reasons,
                'PAUSE',
                0,
            );
        }

        if ($roas !== null && $roas >= $targetRoas && $orders >= $minOrders) {
            $pct = (int) ($settings['scale_up_percentage'] ?? 10);
            $reasons[] = [
                'metric' => 'ROAS',
                'channel' => 'outcome',
                'current' => $roas,
                'target' => $targetRoas,
                'impact' => 'positive',
            ];

            return $this->result(
                CampaignDecisionVerdict::ScaleUp,
                sprintf('Lagged %dd ROAS %.2f meets target.', (int) config('campaign-decisions.monitor.primary_outcome_window_days', 7), $roas),
                $reasons,
                'SCALE_UP',
                $pct,
            );
        }

        if ($roas !== null && $roas < $minRoas && $orders >= $minOrders) {
            $pct = -(int) ($settings['decrease_budget_percentage'] ?? 10);
            $reasons[] = [
                'metric' => 'ROAS',
                'channel' => 'outcome',
                'current' => $roas,
                'target' => $minRoas,
                'impact' => 'negative',
            ];

            return $this->result(
                CampaignDecisionVerdict::DecreaseBudget,
                'ROAS is below minimum but orders are still coming in.',
                $reasons,
                'DECREASE_BUDGET',
                $pct,
            );
        }

        if ($cpa !== null && $cpa > $maxCpa && $orders >= $minOrders) {
            $reasons[] = [
                'metric' => 'CPA',
                'channel' => 'outcome',
                'current' => $cpa,
                'maximum' => $maxCpa,
                'impact' => 'negative',
            ];

            return $this->result(
                CampaignDecisionVerdict::DecreaseBudget,
                'CPA is above maximum on the lag-adjusted window.',
                $reasons,
                'DECREASE_BUDGET',
                -(int) ($settings['decrease_budget_percentage'] ?? 10),
            );
        }

        if ($roas !== null && $roas >= $minRoas && ($cpa === null || $cpa <= $targetCpa)) {
            $reasons[] = [
                'metric' => 'ROAS',
                'channel' => 'outcome',
                'current' => $roas,
                'target' => $minRoas,
                'impact' => 'positive',
            ];

            return $this->result(
                CampaignDecisionVerdict::Maintain,
                'Lagged outcome metrics are within acceptable bands.',
                $reasons,
                'NO_CHANGE',
                0,
            );
        }

        return null;
    }

    /**
     * When ROI sales are missing, score the strongest result count against spend.
     * Priority: conversions, subscriptions, installs or bookings, leads, arb_orders.
     *
     * @param  array<string, mixed>  $outcome
     * @param  array<string, float|int>  $settings
     * @param  list<array<string, mixed>>  $reasons
     * @return array{
     *     verdict: CampaignDecisionVerdict,
     *     reason: string,
     *     reasons: list<array<string, mixed>>,
     *     recommended_action: string,
     *     recommended_budget_change_percent: int
     * }|null
     */
    private function evaluateResultCount(array $outcome, float $spend, array $settings, array &$reasons): ?array
    {
        $signal = $this->leadingResultCount($outcome);

        if ($signal === null) {
            return null;
        }

        $goodCost = (float) ($settings['good_cost_per_result'] ?? 2);
        $acceptableCost = (float) ($settings['acceptable_cost_per_result'] ?? 4);
        $cost = round($spend / $signal['count'], 2);
        $metric = $signal['metric'];

        $reasons[] = [
            'metric' => 'cost_per_'.$metric,
            'channel' => 'outcome',
            'current' => $cost,
            'count' => $signal['count'],
            'target' => $goodCost,
            'maximum' => $acceptableCost,
            'impact' => $cost <= $acceptableCost ? 'positive' : 'negative',
        ];

        if ($cost <= $goodCost) {
            return $this->result(
                CampaignDecisionVerdict::Maintain,
                sprintf('No sales. Cost per %s is $%.2f, which is a good sign.', str_replace('_', ' ', $metric), $cost),
                $reasons,
                'NO_CHANGE',
                0,
            );
        }

        if ($cost <= $acceptableCost) {
            return $this->result(
                CampaignDecisionVerdict::Watch,
                sprintf('No sales. Cost per %s is $%.2f, which is still acceptable.', str_replace('_', ' ', $metric), $cost),
                $reasons,
                'NONE',
                0,
            );
        }

        return $this->result(
            CampaignDecisionVerdict::DecreaseBudget,
            sprintf('No sales. Cost per %s is $%.2f, which is above $%.2f.', str_replace('_', ' ', $metric), $cost, $acceptableCost),
            $reasons,
            'DECREASE_BUDGET',
            -(int) ($settings['decrease_budget_percentage'] ?? 10),
        );
    }

    /**
     * @param  array<string, mixed>  $outcome
     * @return array{metric: string, count: float}|null
     */
    private function leadingResultCount(array $outcome): ?array
    {
        $conversions = (float) ($outcome['conversions'] ?? 0);

        if ($conversions > 0) {
            return ['metric' => 'conversions', 'count' => $conversions];
        }

        $subscriptions = (float) ($outcome['subscriptions'] ?? 0);

        if ($subscriptions > 0) {
            return ['metric' => 'subscriptions', 'count' => $subscriptions];
        }

        $installs = (float) ($outcome['installs'] ?? 0);
        $bookings = (float) ($outcome['bookings'] ?? 0);

        if ($installs > 0 || $bookings > 0) {
            if ($bookings > $installs) {
                return ['metric' => 'bookings', 'count' => $bookings];
            }

            return ['metric' => 'installs', 'count' => $installs];
        }

        $leads = (float) ($outcome['leads'] ?? 0);

        if ($leads > 0) {
            return ['metric' => 'leads', 'count' => $leads];
        }

        $arbOrders = (float) ($outcome['arb_orders'] ?? 0);

        if ($arbOrders > 0) {
            return ['metric' => 'arb_orders', 'count' => $arbOrders];
        }

        return null;
    }

    /**
     * @param  list<array<string, mixed>>  $reasons
     * @return array{
     *     verdict: CampaignDecisionVerdict,
     *     reason: string,
     *     reasons: list<array<string, mixed>>,
     *     recommended_action: string,
     *     recommended_budget_change_percent: int
     * }
     */
    private function result(
        CampaignDecisionVerdict $verdict,
        string $reason,
        array $reasons,
        string $action,
        int $budgetPercent,
    ): array {
        return [
            'verdict' => $verdict,
            'reason' => $reason,
            'reasons' => $reasons,
            'recommended_action' => $action,
            'recommended_budget_change_percent' => $budgetPercent,
        ];
    }
}
