<?php

namespace App\Library\CampaignDecision;

class CampaignDecisionConfidence
{
    /**
     * @param  array<string, mixed>  $combined
     * @param  array<string, mixed>  $channels
     */
    public function score(CampaignPerformanceContext $context, array $combined, array $channels): float
    {
        $score = 0.35;
        $day = $channels['day'] ?? [];
        $spend = (float) ($day['spend'] ?? 0);
        $clicks = (int) ($day['clicks'] ?? 0);

        if ($spend >= (float) ($context->settings['minimum_spend'] ?? 25)) {
            $score += 0.2;
        }

        if ($clicks >= (int) ($context->settings['minimum_clicks'] ?? 20)) {
            $score += 0.15;
        }

        if (($context->affiliate['matched'] ?? false) === true) {
            $score += 0.2;
        }

        if (($context->capabilities['ingest_healthy'] ?? false) === true) {
            $score += 0.1;
        }

        if (count($combined['reasons'] ?? []) >= 2) {
            $score += 0.05;
        }

        return round(min(0.99, max(0.1, $score)), 2);
    }
}
