<?php

declare(strict_types=1);

namespace PpPhp\Core\Build;

final class BuildResult
{
    public function __construct(
        public readonly bool $success,
        public readonly string $message,
        public readonly int $filesGenerated = 0,
        public readonly string $outputDir = '',
        public readonly float $duration = 0,
        public readonly string $size = '0 B',
        public readonly array $pages = [],
        public readonly array $routes = [],
        public readonly array $artifacts = [],
        public readonly array $logs = [],
    ) {}

    public static function success(string $message, array $data = []): self
    {
        return new self(
            success: true,
            message: $message,
            filesGenerated: $data['filesGenerated'] ?? 0,
            outputDir: $data['outputDir'] ?? '',
            duration: $data['duration'] ?? 0,
            size: $data['size'] ?? '0 B',
            pages: $data['pages'] ?? [],
            routes: $data['routes'] ?? [],
            artifacts: $data['artifacts'] ?? [],
            logs: $data['logs'] ?? [],
        );
    }

    public static function error(string $message, array $logs = []): self
    {
        return new self(
            success: false,
            message: $message,
            logs: $logs,
        );
    }

    public function toArray(): array
    {
        return [
            'success' => $this->success,
            'message' => $this->message,
            'filesGenerated' => $this->filesGenerated,
            'outputDir' => $this->outputDir,
            'duration' => $this->duration,
            'size' => $this->size,
            'pages' => $this->pages,
            'routes' => count($this->routes),
        ];
    }
}
