<?php

namespace WPSN\Cli;

use WPSN\Core\Container;

defined('ABSPATH') || exit;

/**
 * Audit CLI commands.
 */
final class AuditCommand
{
    private Container $container;

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

    /**
     * Run a synchronous SQL-based audit scan (no queue/cron).
     *
     * ## OPTIONS
     *
     * [--no-terms]
     * : Do not scan taxonomy terms.
     *
     * [--no-social]
     * : Skip checking social image/og image validity.
     *
     * ## EXAMPLES
     *
     *     wp wpsn audit sql
     *     wp wpsn audit sql --no-terms
     */
    public function sql(array $args, array $assocArgs): void
    {
        $includeTerms = empty($assocArgs['no-terms']);
        $checkSocial = empty($assocArgs['no-social']);

        /** @var \WPSN\Core\Settings $settings */
        $settings = $this->container->get('settings');
        /** @var \WPSN\Core\Logger $logger */
        $logger = $this->container->get('logger');

        $issues = new \WPSN\Modules\Audit\AuditIssueRepository($logger);
        $scans = new \WPSN\Modules\Audit\AuditScanRepository($logger);
        $scanIssues = new \WPSN\Modules\Audit\AuditScanIssueRepository($logger);
        $scanner = new \WPSN\Modules\Audit\AuditSqlScanner($issues, $scans, $scanIssues, $settings, $logger);

        \WP_CLI::log('Running SQL audit scan...');
        $res = $scanner->run($includeTerms, $checkSocial);

        $scanId = isset($res['scan_id']) ? (int) $res['scan_id'] : 0;
        $total = isset($res['issues_total']) ? (int) $res['issues_total'] : 0;

        \WP_CLI::success(sprintf('Audit completed. Scan #%d, issues: %d', $scanId, $total));
    }
}
