<?php

namespace App\SiteReadiness\Checks;

use App\SiteReadiness\CheckResult;
use App\SiteReadiness\ReadinessCheck;
use App\SiteReadiness\Status;

class PaymentGatewayLiveModeCheck implements ReadinessCheck
{
    public function group(): string { return 'Integrations'; }
    public function label(): string { return 'Payment gateways in live mode'; }
    public function hint(): string { return 'Enable a payment gateway and switch it to live/production mode before launch.'; }

    public function run(): CheckResult
    {
        $prod    = config('app.env') === 'production';
        $enabled = [];
        $enabledInTest = [];

        // Judopay
        if ((bool) config('judopay.enabled')) {
            $enabled[] = 'Judopay';
            if ((bool) config('judopay.testmode')) {
                $enabledInTest[] = 'Judopay';
            }
        }

        // RyftPay
        if ((bool) config('ryftpay.enabled')) {
            $enabled[] = 'RyftPay';
            if ((bool) config('ryftpay.testmode')) {
                $enabledInTest[] = 'RyftPay';
            }
        }

        // EvoPay (best-effort via constants)
        if (defined('EVOPAY_LIVE_MERCHANT_ID') && \EVOPAY_LIVE_MERCHANT_ID !== '') {
            $enabled[] = 'EvoPay';
            if (defined('EVOPAY_TEST_MODE') && \EVOPAY_TEST_MODE) {
                $enabledInTest[] = 'EvoPay';
            }
        }

        // Realex (best-effort via constants)
        if (defined('REALEX_MERCHANT_ID') && \REALEX_MERCHANT_ID !== '') {
            $enabled[] = 'Realex';
            if (defined('DEV_MODE') && \DEV_MODE) {
                $enabledInTest[] = 'Realex';
            }
        }

        if (empty($enabled)) {
            return new CheckResult(
                $prod ? Status::FAIL : Status::WARN,
                'No payment gateway is enabled/configured.',
            );
        }

        if (!empty($enabledInTest)) {
            return new CheckResult(
                $prod ? Status::FAIL : Status::WARN,
                'Enabled gateway(s) in test mode: ' . implode(', ', $enabledInTest) . '.',
            );
        }

        return new CheckResult(
            Status::OK,
            'Live gateway(s): ' . implode(', ', $enabled) . '.',
        );
    }
}
