<?php

namespace Storeplugin\PDFInvoice\Framework\Forms\Internals;

defined('ABSPATH') || exit;

/**
 * Fluent panel builder for tab-less SettingPanel
 */
class Panel
{
    protected $title;
    protected $description;
    protected $panels = [];

    /**
     * Create a new Panel instance
     *
     * @param string $title       Panel title
     * @param string $description Panel description
     * @return self
     */
    public static function create($title, $description = '')
    {
        return new self($title, $description);
    }

    /**
     * Constructor
     *
     * @param string $title       Panel title
     * @param string $description Panel description
     */
    private function __construct($title, $description = '')
    {
        $this->title = $title;
        $this->description = $description;
    }

    /**
     * Add a panel/card to the group
     *
     * @param array $config Panel configuration with title, icon, and fields
     * @return self
     */
    public function addPanel(array $config)
    {
        $this->panels[] = [
            'title'            => $config['title'] ?? 'Settings',
            'icon'             => $config['icon'] ?? 'admin-generic',
            'layout'           => $config['layout'] ?? 'vertical',
            'default_col_span' => $config['default_col_span'] ?? 6,
            'fields'           => $config['fields'] ?? [],
        ];

        return $this;
    }

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

    /**
     * Get the panel description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

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

    /**
     * Convert panel to array format
     *
     * @return array
     */
    public function toArray()
    {
        return [
            'id'          => \sanitize_title($this->title),
            'title'       => $this->title,
            'description' => $this->description,
            'panels'      => $this->panels,
        ];
    }
}
