<?php

namespace App\Services\Moderation;

use App\Data\Moderation\ModerationResultData;

class ModerationPriorityService
{
    /**
     * @param array<string, mixed> $context
     * @param array<string, mixed> $policy
     * @param array<string, mixed> $suggestion
     */
    public function score(ModerationResultData $result, array $context = [], array $policy = [], array $suggestion = []): array
    {
        $score = $result->score;
        $score += $result->isSuspicious() ? 10 : 0;
        $score += $result->autoHideRecommended ? 25 : 0;
        $score += max(0, (int) ($result->userRiskScore ?? 0) / 2);
        $score += (int) ($policy['priority_bonus'] ?? 0);
        $score += max(0, (int) (($suggestion['confidence'] ?? 0) / 5));
        $score += ! empty($context['is_publicly_exposed']) ? 12 : 0;
        $score += ! empty($result->matchedDomains) ? 10 : 0;
        $score += isset($result->ruleHits['blocked_domain']) ? 18 : 0;
        $score += isset($result->ruleHits['near_duplicate_campaign']) ? 14 : 0;

        $bucket = match (true) {
            $score >= 140 => 'urgent',
            $score >= 95 => 'high',
            $score >= 50 => 'priority',
            default => 'standard',
        };

        $escalation = match ($bucket) {
            'urgent' => 'urgent',
            'high' => 'escalated',
            'priority' => 'review_required',
            default => 'none',
        };

        return [
            'priority_score' => $score,
            'review_bucket' => $bucket,
            'escalation_status' => $escalation,
        ];
    }
}