<?php

namespace App\Services\Moderation\Rules;

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

class NearDuplicateCampaignRule implements ModerationRuleInterface
{
    public function analyze(string $content, string $normalized, array $context = []): array
    {
        $domains = (array) ($context['extracted_domains'] ?? []);
        $duplicates = app(DuplicateDetectionService::class)->nearDuplicateCount($content, $context, $domains);

        if ($duplicates < 2) {
            return [];
        }

        return [[
            'rule' => 'near_duplicate_campaign',
            'score' => app('config')->get('content_moderation.weights.near_duplicate_campaign', 30),
            'reason' => 'Appears to match an existing spam campaign template (' . $duplicates . ' similar item(s))',
            'links' => (array) ($context['extracted_urls'] ?? []),
            'domains' => $domains,
            'keywords' => [],
        ]];
    }
}