<?php

namespace Acms\Services\SiteSeed\Logger;

use Acms\Services\SiteSeed\Contracts\SiteSeedLogger;
use Throwable;

/**
 * Web UI 向け: 内部バッファにログを貯めておき、テンプレートへ差し込む Logger
 *
 * 現時点では実質 CollectingSiteSeedLogger と同じだが、将来的に Tpl への
 * 書き込みを行うための差し替え先として存在する。
 */
final class WebSiteSeedLogger implements SiteSeedLogger
{
    /** @var list<string> */
    private array $infos = [];

    /** @var list<string> */
    private array $warnings = [];

    /** @var list<array{message: string, exception: \Throwable|null}> */
    private array $errors = [];

    public function info(string $message): void
    {
        $this->infos[] = $message;
    }

    public function progress(string $stage, int $current, int $total): void
    {
        $this->infos[] = sprintf('[%d/%d] %s', $current, $total, $stage);
    }

    public function warning(string $message): void
    {
        $this->warnings[] = $message;
    }

    public function error(string $message, ?Throwable $e = null): void
    {
        $this->errors[] = ['message' => $message, 'exception' => $e];
    }

    /**
     * @return list<string>
     */
    public function infos(): array
    {
        return $this->infos;
    }

    /**
     * @return list<string>
     */
    public function warnings(): array
    {
        return $this->warnings;
    }

    /**
     * @return list<array{message: string, exception: \Throwable|null}>
     */
    public function errors(): array
    {
        return $this->errors;
    }
}
