<?php
namespace App\Services\Moderation;

class LinkScanner
{
    public function __construct(private array $trusted, private array $blocked = ['bit.ly', 'tinyurl.com', 'crack.example', 'adult.example'])
    {
    }

    public function scan(string $url): array
    {
        $host = strtolower((string) parse_url($url, PHP_URL_HOST));
        $host = preg_replace('/^www\./', '', $host);
        if (!$host) return ['status' => 'blocked', 'risk_score' => 100, 'message' => 'Gecersiz URL'];
        if (!str_starts_with($url, 'https://')) return ['status' => 'suspicious', 'risk_score' => 55, 'message' => 'HTTPS olmayan link'];
        if (in_array($host, $this->blocked, true)) return ['status' => 'blocked', 'risk_score' => 95, 'message' => 'Kara listedeki domain'];
        foreach ($this->trusted as $domain) {
            if ($host === $domain || str_ends_with($host, '.' . $domain)) return ['status' => 'safe', 'risk_score' => 5, 'message' => 'Guvenilir domain'];
        }
        return ['status' => 'needs_review', 'risk_score' => 60, 'message' => 'Bilinmeyen domain admin onayi ister'];
    }
}
