<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class CrmProductionCheck extends Command
{
    protected $signature = 'crm:production-check';
    protected $description = 'Check critical HIGHRA CRM production configuration';

    public function handle(): int
    {
        $checks = [
            'APP_DEBUG=false' => filter_var(env('APP_DEBUG', true), FILTER_VALIDATE_BOOLEAN) === false,
            'APP_KEY configured' => filled(config('app.key')),
            'Database reachable' => $this->databaseOk(),
            'Meta credentials configured' => filled(env('META_APP_ID')) && filled(env('META_APP_SECRET')) && filled(env('META_VERIFY_TOKEN')),
            'WhatsApp credentials configured' => filled(env('WHATSAPP_PHONE_NUMBER_ID')) && filled(env('WHATSAPP_ACCESS_TOKEN')),
        ];

        foreach ($checks as $label => $ok) {
            $this->line(($ok ? 'PASS ' : 'FAIL ') . $label);
        }

        return in_array(false, $checks, true) ? self::FAILURE : self::SUCCESS;
    }

    private function databaseOk(): bool
    {
        try {
            DB::connection()->getPdo();
            return true;
        } catch (\Throwable) {
            return false;
        }
    }
}
