<?php

declare(strict_types=1);

namespace Builder\Core\Foundation\Definition;

final class Section
{
    private string $label;

    private ?string $description = null;

    private bool $collapsible = false;

    private int $priority = 0;

    private bool $sidebar = false;

    private ?string $tabGroup = null;

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

    private ?string $viewPermission = null;

    private ?string $updatePermission = null;

    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 collapsible(bool $value = true): self
    {
        $this->collapsible = $value;

        return $this;
    }

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

        return $this;
    }

    public function sidebar(bool $value = true): self
    {
        $this->sidebar = $value;

        return $this;
    }

    public function tab(string $group): self
    {
        $this->tabGroup = $group;

        return $this;
    }

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

        return $this;
    }

    public function canView(?string $permission): self
    {
        $this->viewPermission = $permission;

        return $this;
    }

    public function canUpdate(?string $permission): self
    {
        $this->updatePermission = $permission;

        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 isCollapsible(): bool
    {
        return $this->collapsible;
    }

    public function getPriority(): int
    {
        return $this->priority;
    }

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

    public function getViewPermission(): ?string
    {
        return $this->viewPermission;
    }

    public function getUpdatePermission(): ?string
    {
        return $this->updatePermission;
    }

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