<?php

namespace Acms\Services\SiteSeed;

/**
 * SiteSeed 実行結果 (不変 DTO)
 */
final class SiteSeedResult
{
    /**
     * @param array<string, array<int|string, int|string>> $ids 旧 ID → 新 ID のマッピング (テーブル別)
     * @param array<string, int> $counts 投入件数 (テーブル別)
     * @param list<string> $warnings 非致命的な警告メッセージ
     * @param list<SiteSeedResult> $related 関連 bin の結果 (Installer が複数 bin 投入時に詰める)
     */
    public function __construct(
        public readonly string $binName,
        public readonly int $blogId,
        public readonly array $ids,
        public readonly array $counts,
        public readonly float $elapsedSec,
        public readonly array $warnings = [],
        public readonly array $related = [],
    ) {
    }

    /**
     * related を差し替えた新しいインスタンスを返す (immutable update)
     *
     * @param list<SiteSeedResult> $related
     */
    public function withRelated(array $related): self
    {
        return new self(
            binName: $this->binName,
            blogId: $this->blogId,
            ids: $this->ids,
            counts: $this->counts,
            elapsedSec: $this->elapsedSec,
            warnings: $this->warnings,
            related: $related,
        );
    }

    /**
     * JSON 出力用の連想配列に変換
     *
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'status' => 'ok',
            'bin' => $this->binName,
            'blog_id' => $this->blogId,
            'counts' => $this->counts,
            'elapsed_sec' => $this->elapsedSec,
            'warnings' => $this->warnings,
            'ids_generated' => $this->ids,
            'related' => array_map(
                static fn (SiteSeedResult $r): array => $r->toArray(),
                $this->related,
            ),
        ];
    }
}
