<?php
namespace PrestaShop\Module\LCVPrestaTheme\Config;

use Category;
use Context;
use Link;


class ConfigMenuParent
{
    public $menu;  // int ou array enrichi
    public $isVisible = true;
    public $id ;

    /** Couleur personnalisée du lien parent (hex). Vide = couleur par défaut du thème. */
    public $color = '';

    /** Graisse de la police du lien parent (300..700). Vide = graisse par défaut. */
    public $fontWeight = '';
  
    
 

    /**
     * enrichit le menu avec les données de la catégorie
     */
    public function enrich($action): void
    {
        $existingColumn = [];
        $existingImage  = "";

        if($action == 'edit'){
            $config = ConfigTheme::load();

            foreach ($config->configMenuParent as $parent) {
                if ($parent->id == $this->id) {
                    $existingColumn = $parent->menu['columns'] ?? [];
                    // On conserve l'image déjà uploadée : sans ce report, éditer un
                    // parent custom sans re-uploader effacerait son image.
                    $existingImage  = $parent->menu['image'] ?? "";
                    break;
                }
            }

        }

            if ($this->menu_type === 'custom') {

                $this->menu = [
                    'id'      => $this->id,
                    'name'    => $this->custom_label,
                    'image'   => $existingImage,
                    'url'     => $this->custom_url,
                    'type'    => 'custom',
                    'columns' => $existingColumn,
                ];
                unset($this->brand_label, $this->menu_type);

            } elseif ($this->menu_type === 'brand') {

                $link = new Link();
                $this->menu = [
                    'id'        => $this->id,
                    'source_id' => $this->id,
                    'name'      => ($this->brand_label !== '' && $this->brand_label !== null) ? $this->brand_label : 'Marques',
                    'url'       => $link->getPageLink('manufacturer'),
                    'type'      => 'brand',
                    'columns'   => [],
                ];
                unset($this->custom_label, $this->custom_url, $this->image, $this->brand_label, $this->menu_type);

            } else {

                // Mode category (comportement original)
                $link     = new Link();
                $id_lang  = Context::getContext()->language->id;
                $menuId   = is_array($this->menu) ? $this->menu['id'] : (int) $this->menu;
                $category = new Category($menuId, $id_lang);

                $this->menu = [
                    'id'        => $category->id,
                    'source_id' => $this->id,
                    'name'      => $category->name,
                    'url'       => $link->getCategoryLink($category),
                    'type'      => 'category',
                    'columns'   => $existingColumn,
                ];
                unset($this->custom_label, $this->custom_url, $this->image, $this->brand_label, $this->menu_type);

            }
        

       

        
        
    }

    public function addColumn(array $column): void
    {
        if (!isset($this->menu['columns'])) {
            $this->menu['columns'] = [];
        }
        $this->menu['columns'][] = $column;
    }
}
