<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CheckRazorpayConfig extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'razorpay:check';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Check Razorpay configuration';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('Checking Razorpay Configuration...');
        $this->newLine();

        // Check all possible environment variable names
        $envVars = [
            'RZP_KEY' => env('RZP_KEY'),
            'RZP_SECRET' => env('RZP_SECRET'),
            'RAZORPAY_KEY' => env('RAZORPAY_KEY'),
            'RAZORPAY_SECRET' => env('RAZORPAY_SECRET'),
            'RAZORPAY_KEY_ID' => env('RAZORPAY_KEY_ID'),
            'RAZORPAY_KEY_SECRET' => env('RAZORPAY_KEY_SECRET'),
            'RAZORPAY_SECRET_KEY' => env('RAZORPAY_SECRET_KEY'),
        ];

        $this->table(
            ['Environment Variable', 'Value Status', 'Value Preview'],
            collect($envVars)->map(function ($value, $key) {
                $status = !empty($value) ? '<fg=green>SET</>' : '<fg=red>NOT SET</>';
                $preview = !empty($value) ? substr($value, 0, 20) . (strlen($value) > 20 ? '...' : '') : '-';
                return [$key, $status, $preview];
            })->toArray()
        );

        $this->newLine();

        // Check config values
        $configKey = config('services.razorpay.key');
        $configSecret = config('services.razorpay.secret');

        $this->info('Configuration Values:');
        $this->line('Key: ' . (!empty($configKey) ? '<fg=green>' . substr($configKey, 0, 20) . '...</>' : '<fg=red>NOT CONFIGURED</>'));
        $this->line('Secret: ' . (!empty($configSecret) ? '<fg=green>' . substr($configSecret, 0, 10) . '****...</>' : '<fg=red>NOT CONFIGURED</>'));

        $this->newLine();

        if (empty($configKey) || empty($configSecret)) {
            $this->error('Razorpay is NOT properly configured!');
            $this->newLine();
            $this->warn('Please add to your .env file one of the following combinations:');
            $this->newLine();
            $this->line('Option 1 (Preferred):');
            $this->line('RZP_KEY=your_key_id');
            $this->line('RZP_SECRET=your_secret_key');
            $this->newLine();
            $this->line('Option 2:');
            $this->line('RAZORPAY_KEY_ID=your_key_id');
            $this->line('RAZORPAY_SECRET_KEY=your_secret_key');
            $this->newLine();
            $this->line('Option 3:');
            $this->line('RAZORPAY_KEY=your_key_id');
            $this->line('RAZORPAY_SECRET=your_secret_key');
            $this->newLine();
            $this->warn('Then run: php artisan config:clear');
            return 1;
        }

        $this->info('Razorpay configuration looks good!');
        return 0;
    }
}
