<?php

namespace WPSN\Cli;

use WPSN\Core\Container;
use WPSN\Modules\Diagnostics\QualityGateService;
use WPSN\Modules\Diagnostics\ReleaseSmokeMatrixService;

defined('ABSPATH') || exit;

/**
 * Smoke / release checks.
 */
final class SmokeCommand
{
    private Container $container;

    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    /**
     * Run WP SEO Next smoke checks (Quality Gate).
     *
     * ## OPTIONS
     *
     * [--json]
     * : Output full JSON payload.
     *
     * [--release]
     * : Run the release smoke matrix (frontend cases + rollback/tooling readiness).
     *
     * ## EXAMPLES
     *
     *     wp wpsn smoke
     *     wp wpsn smoke --json
     *     wp wpsn smoke --release
     */
    public function __invoke(array $args, array $assocArgs): void
    {
        $asJson = !empty($assocArgs['json']);
        $release = !empty($assocArgs['release']);

        if ($release) {
            $svc = new ReleaseSmokeMatrixService($this->container);
            $res = $svc->run(['source' => 'wp_cli', 'trigger' => 'wp_cli']);
            $svc->save($res);
        } else {
            $svc = new QualityGateService($this->container);
            $res = $svc->runAllChecks(['source' => 'wp_cli']);
        }

        $severity = isset($res['severity']) ? (string) $res['severity'] : 'info';

        if ($asJson) {
            \WP_CLI::log((string) wp_json_encode($res, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
        } else {
            \WP_CLI::log($release ? 'WP SEO Next release smoke matrix' : 'WP SEO Next smoke checks');
            \WP_CLI::log('Plugin version: ' . (string) ($res['plugin_version'] ?? ''));
            \WP_CLI::log('Severity: ' . $severity);

            if ($release) {
                $counts = isset($res['counts']) && is_array($res['counts']) ? $res['counts'] : [];
                \WP_CLI::log(sprintf('Counts: ok=%d info=%d warn=%d critical=%d', (int) ($counts['ok'] ?? 0), (int) ($counts['info'] ?? 0), (int) ($counts['warn'] ?? 0), (int) ($counts['critical'] ?? 0)));
            }

            $checks = isset($res['checks']) && is_array($res['checks']) ? $res['checks'] : [];
            foreach ($checks as $k => $c) {
                if (!is_array($c)) {
                    continue;
                }
                $label = (string) $k;
                $ok = !empty($c['ok']);
                $sev = isset($c['severity']) ? (string) $c['severity'] : 'info';
                $sum = isset($c['summary']) ? (string) $c['summary'] : '';

                $prefix = $ok ? '[OK]' : '[!!]';
                \WP_CLI::log(sprintf('%s %-22s %-10s %s', $prefix, $label, $sev, $sum));
            }
        }

        if ($severity === 'critical') {
            \WP_CLI::error($release
                ? 'Release smoke matrix failed (critical). Open Tools → Quality Gate and inspect the release matrix.'
                : 'Smoke checks failed (critical). Open Tools → Compatibility (Diagnostics) and fix the failing checks.');
        }

        if ($severity === 'warn') {
            \WP_CLI::warning($release ? 'Release smoke matrix finished with warnings.' : 'Smoke checks finished with warnings.');
            return;
        }

        \WP_CLI::success($release ? 'Release smoke matrix OK.' : 'Smoke checks OK.');
    }
}
