<?php

namespace App\Services\Moderation;

use App\Enums\ModerationContentType;

class ModerationPolicyEngineService
{
    /**
     * @param array<string, mixed> $context
     * @param array<string, mixed> $riskAssessment
     * @return array<string, mixed>
     */
    public function resolve(array $context, array $riskAssessment = []): array
    {
        $policies = (array) app('config')->get('content_moderation.policies', []);
        $contentType = ModerationContentType::tryFrom((string) ($context['content_type'] ?? ''));
        $accountAgeDays = (int) data_get($riskAssessment, 'signals.account_age_days', 0);
        $riskScore = (int) ($riskAssessment['risk_score'] ?? 0);
        $hasLinks = ! empty($context['extracted_urls'] ?? []) || ! empty($context['extracted_domains'] ?? []);

        $name = 'default';

        if ($riskScore >= 70 || ($accountAgeDays > 0 && $accountAgeDays < 14)) {
            $name = 'new_user_strict_mode';
        } elseif ($riskScore <= 8 && $accountAgeDays >= 180) {
            $name = 'trusted_user_relaxed_mode';
        }

        if ($contentType === ModerationContentType::ArtworkComment && $riskScore >= 45) {
            $name = 'comments_high_volume_antispam';
        }

        if ($hasLinks && in_array($contentType, [
            ModerationContentType::UserProfileLink,
            ModerationContentType::CollectionDescription,
            ModerationContentType::CollectionTitle,
            ModerationContentType::StoryContent,
            ModerationContentType::StoryTitle,
            ModerationContentType::CardText,
            ModerationContentType::CardTitle,
        ], true)) {
            $name = 'strict_seo_protection';
        }

        $policy = $policies[$name] ?? ($policies['default'] ?? []);
        $policy['name'] = $name;

        return $policy;
    }
}