<?php

namespace Tests\Unit\SiteReadiness\Checks;

use App\SiteReadiness\Checks\AnalyticsIdCheck;
use App\SiteReadiness\Checks\PaymentGatewayLiveModeCheck;
use App\SiteReadiness\Checks\RecaptchaConfiguredCheck;
use App\SiteReadiness\Checks\RobotsTxtCheck;
use App\SiteReadiness\Status;
use Tests\TestCase;

/**
 * Unit tests for the Integrations group checks.
 *
 * NOTE: The legacy bootstrap (core/includes/header.inc.php) defines
 * REALEX_MERCHANT_ID and EVOPAY_LIVE_MERCHANT_ID as empty-string constants
 * for the test environment. PaymentGatewayLiveModeCheck now requires a
 * non-empty value, so those constants are correctly ignored in tests and
 * the config-based (Judopay/RyftPay) branches determine the result.
 */
class IntegrationsChecksTest extends TestCase
{
    // ------------------------------------------------------------------
    // PaymentGatewayLiveModeCheck
    // ------------------------------------------------------------------

    public function test_enabled_judopay_in_test_mode_fails_in_production(): void
    {
        config([
            'app.env'          => 'production',
            'judopay.enabled'  => true,
            'judopay.testmode' => true,
            'ryftpay.enabled'  => false,
            'ryftpay.testmode' => false,
        ]);

        $result = (new PaymentGatewayLiveModeCheck())->run();

        $this->assertSame(Status::FAIL, $result->status);
        $this->assertStringContainsStringIgnoringCase('judopay', $result->detail);
    }

    public function test_enabled_judopay_live_is_ok(): void
    {
        // With empty-string Realex/EvoPay constants (starter default) and Judopay
        // enabled in live mode, the check must return OK in production.
        config([
            'app.env'          => 'production',
            'judopay.enabled'  => true,
            'judopay.testmode' => false,
            'ryftpay.enabled'  => false,
        ]);

        $result = (new PaymentGatewayLiveModeCheck())->run();

        $this->assertSame(Status::OK, $result->status);
    }

    public function test_no_config_gateway_enabled_does_not_report_live_gateways(): void
    {
        // When both config-based gateways are disabled the result must be FAIL
        // in production (either "No payment gateway" or a constant-detected
        // gateway in test mode - both are FAIL). We assert the status only,
        // since the detail string depends on which constants the bootstrap sets.
        config([
            'app.env'          => 'production',
            'judopay.enabled'  => false,
            'judopay.testmode' => false,
            'ryftpay.enabled'  => false,
            'ryftpay.testmode' => false,
        ]);

        $result = (new PaymentGatewayLiveModeCheck())->run();

        $this->assertSame(Status::FAIL, $result->status);
    }

    // ------------------------------------------------------------------
    // RecaptchaConfiguredCheck
    // ------------------------------------------------------------------

    public function test_recaptcha_unconfigured_warns(): void
    {
        config([
            'recaptcha.site_key'   => '',
            'recaptcha.secret_key' => '',
        ]);

        $result = (new RecaptchaConfiguredCheck())->run();

        $this->assertSame(Status::WARN, $result->status);
    }

    // ------------------------------------------------------------------
    // AnalyticsIdCheck
    // ------------------------------------------------------------------

    public function test_analytics_gtm_id_set_is_ok(): void
    {
        config(['site.gtm_id' => 'GTM-XXXXXXX']);

        $result = (new AnalyticsIdCheck())->run();

        $this->assertSame(Status::OK, $result->status);
    }

    public function test_analytics_gtm_id_empty_warns(): void
    {
        config(['site.gtm_id' => '']);

        $result = (new AnalyticsIdCheck())->run();

        $this->assertSame(Status::WARN, $result->status);
    }

    // ------------------------------------------------------------------
    // RobotsTxtCheck
    // ------------------------------------------------------------------

    public function test_robots_txt_missing_warns(): void
    {
        // public/robots.txt does not exist in this repo, so the check
        // returns WARN for the missing-file case.
        $result = (new RobotsTxtCheck())->run();

        $this->assertSame(Status::WARN, $result->status);
        $this->assertStringContainsStringIgnoringCase('no robots.txt', $result->detail);
    }

    // ------------------------------------------------------------------
    // RecaptchaConfiguredCheck — happy path
    // ------------------------------------------------------------------

    public function test_recaptcha_both_keys_set_is_ok(): void
    {
        config([
            'recaptcha.site_key'   => 'site-key-value',
            'recaptcha.secret_key' => 'secret-key-value',
        ]);

        $result = (new RecaptchaConfiguredCheck())->run();

        $this->assertSame(Status::OK, $result->status);
    }
}
