<?php

declare(strict_types=1);

namespace App\Support;

use App\Http\Middleware\AllowLivewireUpdateDuringSystemUpdateMaintenance;

/**
 * 系统进入维护模式时传给 artisan down 的选项（预渲染友好维护页）。
 */
final class DberpMaintenanceDownCommandOptions
{
    /**
     * 供 DownCommand::excludedPaths() 写入 framework/down；勿在 AppServiceProvider::boot 常驻注册。
     */
    public function applyExceptPathsForDownCommand(): void
    {
        AllowLivewireUpdateDuringSystemUpdateMaintenance::except(
            app(DberpMaintenanceDownExceptPaths::class)->paths(),
        );
    }

    public function clearExceptPathsRegistration(): void
    {
        AllowLivewireUpdateDuringSystemUpdateMaintenance::flushState();
    }

    /**
     * @return array<string, string|int>
     */
    public function artisanArguments(): array
    {
        $retrySeconds = max(30, (int) config('dberp.system_update.maintenance_page.retry_seconds', 60));
        $refreshSeconds = max(15, (int) config('dberp.system_update.maintenance_page.refresh_seconds', 30));

        return [
            '--render' => (string) config('dberp.system_update.maintenance_page.render_view', 'errors.maintenance'),
            '--retry' => $retrySeconds,
            '--refresh' => $refreshSeconds,
        ];
    }

    /**
     * @return list<string>
     */
    public function processArgvSuffix(): array
    {
        $arguments = [];

        foreach ($this->artisanArguments() as $option => $value) {
            $arguments[] = $option.'='.$value;
        }

        return $arguments;
    }
}
