<?php

declare(strict_types=1);

namespace SiteFort\Modules\Scanner\Service;

if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * Class ReputationLookup
 *
 * Standalone reputation query helpers for use outside the scan pipeline.
 * All checks proxy through the Cloud API.
 */
class ReputationLookup {

    /**
     * Check IP reputation via Cloud API.
     *
     * @param string $ip IP address to check.
     * @return array ip_reputation payload or empty array on error.
     */
    public function check_ip( string $ip ): array {
        if ( ! filter_var( $ip, FILTER_VALIDATE_IP ) ) {
            return [];
        }

        if ( ! class_exists( 'SiteFort\\Modules\\Cloud\\Services\\CloudScanner' ) ) {
            return [];
        }

        $cloud  = new \SiteFort\Modules\Cloud\Services\CloudScanner();
        $result = $cloud->check_reputation( $ip, '' );

        if ( is_wp_error( $result ) ) {
            return [];
        }

        return $result['ip_reputation'] ?? [];
    }

    /**
     * Check if a domain is blacklisted via Cloud API.
     *
     * @param string $domain Domain to check.
     * @return bool True if blacklisted, false if clean or on error.
     */
    public function is_domain_blacklisted( string $domain ): bool {
        if ( empty( $domain ) ) {
            return false;
        }

        if ( ! class_exists( 'SiteFort\\Modules\\Cloud\\Services\\CloudScanner' ) ) {
            return false;
        }

        $cloud  = new \SiteFort\Modules\Cloud\Services\CloudScanner();
        $result = $cloud->check_reputation( '', $domain );

        if ( is_wp_error( $result ) ) {
            return false;
        }

        return ( (int) ( $result['domain_blacklist_count'] ?? 0 ) ) > 0;
    }
}
