<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CheckProductionIntegrations extends Command
{
    protected $signature = 'raseednow:check-integrations {--allow-mock : Allow mock/log drivers for local QA}';

    protected $description = 'Validate payment, fulfillment, email, and SMS integration configuration.';

    public function handle(): int
    {
        $allowMock = $this->option('allow-mock') || app()->environment('local', 'testing');
        $missing = [];

        $this->checkPayment($missing, $allowMock);
        $this->checkFulfillment($missing, $allowMock);
        $this->checkNotifications($missing, $allowMock);

        if ($missing) {
            foreach ($missing as $item) {
                $this->error($item);
            }

            return self::FAILURE;
        }

        $this->info('Integration configuration looks ready.');

        return self::SUCCESS;
    }

    private function checkPayment(array &$missing, bool $allowMock): void
    {
        $driver = config('raseednow.payments.driver');

        if ($driver === 'mock_gateway' && ! $allowMock) {
            $missing[] = 'RASEEDNOW_PAYMENT_DRIVER must not be mock_gateway in production.';
        }

        if ($driver === 'generic_http') {
            $this->requireKeys($missing, [
                'raseednow.payments.generic_http.endpoint' => 'RASEEDNOW_PAYMENT_ENDPOINT',
                'raseednow.payments.generic_http.api_key' => 'RASEEDNOW_PAYMENT_API_KEY',
                'raseednow.payments.generic_http.api_secret' => 'RASEEDNOW_PAYMENT_API_SECRET',
                'raseednow.payments.generic_http.webhook_secret' => 'RASEEDNOW_PAYMENT_WEBHOOK_SECRET',
            ]);
        }
    }

    private function checkFulfillment(array &$missing, bool $allowMock): void
    {
        $driver = config('raseednow.fulfillment.driver');

        if ($driver === 'mock_provider' && ! $allowMock) {
            $missing[] = 'RASEEDNOW_FULFILLMENT_DRIVER must not be mock_provider in production.';
        }

        if ($driver === 'generic_http') {
            $this->requireKeys($missing, [
                'raseednow.fulfillment.generic_http.endpoint' => 'RASEEDNOW_FULFILLMENT_ENDPOINT',
                'raseednow.fulfillment.generic_http.api_key' => 'RASEEDNOW_FULFILLMENT_API_KEY',
                'raseednow.fulfillment.generic_http.api_secret' => 'RASEEDNOW_FULFILLMENT_API_SECRET',
            ]);
        }
    }

    private function checkNotifications(array &$missing, bool $allowMock): void
    {
        if (config('raseednow.notifications.email_driver') === 'log' && ! $allowMock) {
            $missing[] = 'RASEEDNOW_EMAIL_DRIVER should be mail in production.';
        }

        if (config('raseednow.notifications.sms_driver') === 'log' && ! $allowMock) {
            $missing[] = 'RASEEDNOW_SMS_DRIVER should be generic_http or a real SMS driver in production.';
        }

        if (config('raseednow.notifications.sms_driver') === 'generic_http') {
            $this->requireKeys($missing, [
                'raseednow.notifications.sms_http.endpoint' => 'RASEEDNOW_SMS_ENDPOINT',
                'raseednow.notifications.sms_http.api_key' => 'RASEEDNOW_SMS_API_KEY',
                'raseednow.notifications.sms_http.api_secret' => 'RASEEDNOW_SMS_API_SECRET',
            ]);
        }
    }

    /**
     * @param  array<string, string>  $keys
     */
    private function requireKeys(array &$missing, array $keys): void
    {
        foreach ($keys as $configKey => $envKey) {
            if (blank(config($configKey))) {
                $missing[] = $envKey.' is required.';
            }
        }
    }
}
