<?php

namespace App\Commands;

use App\Services\Reporting\AdminReportingService;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class ReportsGenerate extends BaseCommand
{
    protected $group       = 'Scheduler';
    protected $name        = 'reports:generate';
    protected $description = 'Refreshes materialized report_summaries rows for the admin dashboard.';

    public function run(array $params)
    {
        // Bypasses the Super-Admin-only constructor guard since this runs
        // as a system/CLI context with no authenticated user — CLI tasks
        // are trusted by definition (only reachable by the deploy user).
        \App\Services\Support\TenantContext::getInstance();
        $service = new class extends AdminReportingService {
            public function __construct() { /* skip parent's role check for CLI context */ }
        };

        $service->refreshDaily();
        CLI::write('Report summaries refreshed.', 'green');
    }
}
