<?php

namespace App\Services\Moderation\Rules;

use App\Contracts\Moderation\ModerationRuleInterface;
use App\Services\Moderation\ModerationRuleRegistryService;

class SuspiciousKeywordRule implements ModerationRuleInterface
{
    public function analyze(string $content, string $normalized, array $context = []): array
    {
        $registry = app(ModerationRuleRegistryService::class);
        $weights = app('config')->get('content_moderation.weights', []);
        $findings = [];

        $highRiskMatched = [];
        $suspiciousMatched = [];

        foreach ($registry->highRiskKeywords() as $phrase) {
            if (str_contains($normalized, strtolower($phrase))) {
                $highRiskMatched[] = $phrase;
            }
        }

        foreach ($registry->suspiciousKeywords() as $phrase) {
            if (str_contains($normalized, strtolower($phrase))) {
                $suspiciousMatched[] = $phrase;
            }
        }

        if (!empty($highRiskMatched)) {
            $findings[] = [
                'rule'     => 'high_risk_keyword',
                'score'    => ($weights['high_risk_keyword'] ?? 40) * count($highRiskMatched),
                'reason'   => 'Contains high-risk keyword(s): ' . implode(', ', $highRiskMatched),
                'links'    => [],
                'domains'  => [],
                'keywords' => $highRiskMatched,
            ];
        }

        if (!empty($suspiciousMatched)) {
            $findings[] = [
                'rule'     => 'suspicious_keyword',
                'score'    => ($weights['suspicious_keyword'] ?? 25) * count($suspiciousMatched),
                'reason'   => 'Contains suspicious keyword(s): ' . implode(', ', $suspiciousMatched),
                'links'    => [],
                'domains'  => [],
                'keywords' => $suspiciousMatched,
            ];
        }

        return $findings;
    }
}
