<?php

declare(strict_types=1);

namespace App\Services\SupportCenter;

use Illuminate\Support\Carbon;

/**
 * 白名单 Artisan 命令执行结果（不含密钥）。
 */
final class HostSystemUpdateArtisanCommandResult
{
    public function __construct(
        public readonly string $command,
        /** @var list<string> */
        public readonly array $arguments,
        public readonly int $exitCode,
        public readonly string $stdout,
        public readonly string $stderr,
        public readonly Carbon $startedAt,
        public readonly Carbon $finishedAt,
        public readonly int $durationMs,
    ) {}

    public function succeeded(): bool
    {
        return $this->exitCode === 0;
    }

    /**
     * @return array<string, mixed>
     */
    public function toLogContext(): array
    {
        return [
            'command' => $this->command,
            'arguments' => $this->arguments,
            'exit_code' => $this->exitCode,
            'duration_ms' => $this->durationMs,
            'stdout_excerpt' => HostSystemUpdateArtisanOutputSanitizer::excerpt($this->stdout),
            'stderr_excerpt' => HostSystemUpdateArtisanOutputSanitizer::excerpt($this->stderr),
        ];
    }
}
