<?php

namespace Storeplugin\PDFInvoice\Framework\Forms\Internals;

defined('ABSPATH') || exit;

/**
 * Fluent tab builder for SettingPanel
 */
class Tab
{
    protected $id;
    protected $title;
    protected $icon;
    protected $sublinks = [];
    protected $currentIndex = -1;

    /**
     * Create a new Tab instance
     *
     * @param string $id   Tab ID
     * @param string $title Tab title
     * @param string $icon Tab icon
     * @return self
     */
    public static function create($id, $title, $icon)
    {
        return new self($id, $title, $icon);
    }

    /**
     * Constructor
     *
     * @param string $id   Tab ID
     * @param string $title Tab title
     * @param string $icon Tab icon
     */
    private function __construct($id, $title, $icon)
    {
        $this->id = \sanitize_title($id);
        $this->title = $title;
        $this->icon = $icon;
    }

    /**
     * Add a sublink/tab panel
     *
     * @param string $name        Sublink name/title
     * @param string $description Sublink description
     * @return self
     */
    public function tabLink($name, $description = '')
    {
        $this->sublinks[] = [
            'id'          => \sanitize_title($name),
            'title'       => $name,
            'description' => $description,
            'panels'      => [],
        ];
        $this->currentIndex = count($this->sublinks) - 1;

        return $this;
    }

    /**
     * Add a panel to the current sublink
     *
     * @param array $config Panel configuration with title, icon, and fields
     * @return self
     */
    public function addPanel(array $config)
    {
        if ($this->currentIndex < 0) {
            // If no sublink exists, create a default one
            $this->tabLink($this->title);
        }

        $panel = [
            'title'            => $config['title'] ?? 'Settings',
            'icon'             => $config['icon'] ?? 'admin-generic',
            'layout'           => $config['layout'] ?? 'vertical',
            'default_col_span' => $config['default_col_span'] ?? 6,
            'fields'           => $config['fields'] ?? [],
        ];

        $this->sublinks[$this->currentIndex]['panels'][] = $panel;

        return $this;
    }

    /**
     * Get the tab ID
     *
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get the tab title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Get the tab icon
     *
     * @return string
     */
    public function getIcon()
    {
        return $this->icon;
    }

    /**
     * Get all sublinks
     *
     * @return array
     */
    public function getSublinks()
    {
        return $this->sublinks;
    }

    /**
     * Convert tab to array format compatible with SettingPanel
     *
     * @return array
     */
    public function toArray()
    {
        return [
            'id'       => $this->id,
            'title'    => $this->title,
            'icon'     => $this->icon,
            'sublinks' => $this->sublinks,
        ];
    }
}
