<?php

declare(strict_types=1);

namespace PpPhp\Core\Manifest;

final class Manifest
{
    public function __construct(
        private readonly array $data,
        private readonly string $path = ''
    ) {
    }

    public function id(): string
    {
        return trim((string) ($this->data['id'] ?? ''));
    }

    public function type(): string
    {
        return trim((string) ($this->data['type'] ?? ''));
    }

    public function label(): string
    {
        return trim((string) ($this->data['label'] ?? $this->data['name'] ?? $this->id()));
    }

    public function version(): string
    {
        return trim((string) ($this->data['version'] ?? '1.0.0'));
    }

    public function status(): string
    {
        return trim((string) ($this->data['status'] ?? 'active'));
    }

    public function path(): string
    {
        return $this->path;
    }

    public function dependencies(): array
    {
        return is_array($this->data['dependencies'] ?? null) ? $this->data['dependencies'] : [];
    }

    public function assets(): array
    {
        return is_array($this->data['assets'] ?? null) ? $this->data['assets'] : [];
    }

    public function metadata(): array
    {
        return is_array($this->data['metadata'] ?? null) ? $this->data['metadata'] : [];
    }

    public function get(string $key, mixed $default = null): mixed
    {
        return $this->data[$key] ?? $default;
    }

    public function data(): array
    {
        return $this->data;
    }

    public function toArray(): array
    {
        return array_merge($this->data, [
            '_path' => $this->path,
        ]);
    }
}
