<?php

declare(strict_types=1);

namespace Builder\Core\Foundation\Settings;

use Builder\Core\Foundation\Definition\Field;

final class SettingsSection
{
    private string $label;

    private ?string $description = null;

    private int $order = 50;

    /** @var list<Field> */
    private array $fields = [];

    private function __construct(private readonly string $key)
    {
        $this->label = $key;
    }

    public static function make(string $key): self
    {
        return new self($key);
    }

    public function label(string $label): self
    {
        $this->label = $label;

        return $this;
    }

    public function description(string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function order(int $order): self
    {
        $this->order = $order;

        return $this;
    }

    /** @param list<Field> $fields */
    public function fields(array $fields): self
    {
        $this->fields = $fields;

        return $this;
    }

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

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

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function getOrder(): int
    {
        return $this->order;
    }

    /** @return list<Field> */
    public function getFields(): array
    {
        return $this->fields;
    }

    /** @return array<string, mixed> */
    public function toArray(): array
    {
        return [
            'key' => $this->key,
            'label' => $this->label,
            'description' => $this->description,
            'order' => $this->order,
            'fields' => array_map(fn (Field $f): array => $f->toArray(), $this->fields),
        ];
    }
}
