<?php
namespace App\Components\Admin;

use App\Entity\User;
use App\Entity\Sponsor;
use App\Repository\SponsorRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(name: 'AdminSideBar', template: "admin/components/sidebar.html.twig")]
class AdminSideBar
{
    function __construct(private TranslatorInterface $t, private EntityManagerInterface $manager) {
        
    }

    const SEPARATOR = 'separator';

    private $full_menu = [
        [ 'name' => 'admin.sponsors.menu-item',      'domain' => 'admin',    'path' => 'admin_sponsors_list',     'path_params' => null,  'icon' => 'user-tie',           'class' => 'text-dark',   'only_admin' => false ],
        [ 'name' => 'admin.users.menu-item',         'domain' => 'admin',    'path' => 'admin_users_list',        'path_params' => null,  'icon' => 'users',              'class' => 'text-dark',   'only_admin' => true  ],

        [ 'name' => self::SEPARATOR,                 'domain' => '',         'path' => '',                        'path_params' => null,  'icon' => '',                   'class' => '',            'only_admin' => false ],
        
        [ 'name' => 'admin.events.my.menu-item',     'domain' => 'admin',    'path' => 'admin_events_list_by',    'path_params' => ['field' => 'partner', 'value' => 'my'],  'icon' => 'calendar-day',      'class' => 'text-dark',   'only_admin' => false ],
        [ 'name' => 'admin.events.all.menu-item',    'domain' => 'admin',    'path' => 'admin_events_list',       'path_params' => null,  'icon' => 'calendar-days',      'class' => 'text-dark',   'only_admin' => false ],

        [ 'name' => 'admin.event-types.menu-item',   'domain' => 'admin',    'path' => 'admin_typeevents_list',   'path_params' => null,  'icon' => 'calendar',           'class' => 'text-dark',   'only_admin' => false ],
        
        [ 'name' => self::SEPARATOR,                 'domain' => '',         'path' => '',                        'path_params' => null,  'icon' => '',                   'class' => '',            'only_admin' => false ],
        
        [ 'name' => 'admin.settings.pages.whoarewe', 'domain' => 'admin',    'path' => 'admin_whoarewe',          'path_params' => null,  'icon' => 'address-card',       'class' => 'text-dark',   'only_admin' => true ],
        [ 'name' => 'admin.settings.pages.legal',    'domain' => 'admin',    'path' => 'admin_legal',             'path_params' => null,  'icon' => 'scale-balanced',     'class' => 'text-dark',   'only_admin' => true ],
        
        [ 'name' => self::SEPARATOR,                 'domain' => '',         'path' => '',                        'path_params' => null,  'icon' => '',                   'class' => '',            'only_admin' => true ],
        
        [ 'name' => 'admin.user.my-profile.title',   'domain' => 'admin',    'path' => 'admin_user_profile',      'path_params' => null,  'icon' => 'user',               'class' => 'text-dark',   'only_admin' => false ],
        [ 'name' => 'auth.logout.menu',              'domain' => 'security', 'path' => 'auth_logout',             'path_params' => null,  'icon' => 'right-from-bracket', 'class' => 'text-danger', 'only_admin' => false ]
    ];

    public ?User $user = null;

    public function getMenu() : array
    {
        $menu = [];
        if (!$this->user) return $menu;

        $i = 0;
        foreach ($this->full_menu as $item) {

            // dump($item);
            // dump($item["only_admin"]);
            // || !$item->only_admin
            if ($this->user->isAdmin() || !$item["only_admin"]) {
                $menu["item $i"] = [ 
                    'name' => ($item["name"] != self::SEPARATOR) ? $this->t->trans($item["name"], parameters: [ "%partner%" => $this->getSponsor()->getName() ], domain: $item["domain"]) : self::SEPARATOR,
                    'path' => $item["path"], 
                    'path_params' => $item["path_params"], 
                    'icon' => $item["icon"],
                    'class' => $item["class"]
                ];
                $i++;
            }
        }

        // dump($menu);

        return $menu;
    }

    private ?Sponsor  $sponsor = null;
    public function getSponsor() : Sponsor 
    {
        if ($this->sponsor === null && $this->user !== null) {
            $this->sponsor = $this->manager->getRepository(Sponsor::class)->findOneBy(["id" => $this->user->getSponsor()->getId()]);
        }
        return $this->sponsor;
    }
}