<?php
/**
 * BookiFlex Core
 *
 * @package    BookiFlex
 * @subpackage Core
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Core\Admin\Settings;

use Carbon_Fields\Container\Container;

defined( 'ABSPATH' ) || exit;

/**
 * Represents a single settings container (menu item)
 */
class SettingsContainer
{
    private string $id;
    private string $title;
    private string $parent = 'bookiflex';
    private string $layout = 'tabbed-vertical';
    private array $tabs = [];
    private ?string $capability = null;

    public function __construct(string $id, string $title)
    {
        $this->id = $id;
        $this->title = $title;
    }

    public function setParent(string $parent): self
    {
        $this->parent = $parent;
        return $this;
    }

    public function setLayout(string $layout): self
    {
        $this->layout = $layout;
        return $this;
    }

    public function setCapability(string $capability): self
    {
        $this->capability = $capability;
        return $this;
    }

    public function addTab(SettingsTab $tab): self
    {
        $this->tabs[$tab->getId()] = $tab;
        return $this;
    }

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

    public function build(): Container
    {
        $container = Container::make('theme_options', $this->id, $this->title)
            ->set_layout($this->layout)
            ->set_page_file( 'bookiflex-settings' )
            ->set_page_parent($this->parent);

        foreach ($this->tabs as $tab) {
            $container->add_tab(
                $tab->getName(),
                $tab->getFields()
            );
        }

        return $container;
    }

    public function getCapability(): ?string
    {
        return $this->capability;
    }

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