<?php

namespace App\Controller\Admin;

use App\Entity\Pages;
use App\Entity\Projects;
use App\Entity\Quotes;
use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;

class DashboardController extends AbstractDashboardController
{
    #[isGranted(data: 'ROLE_ADMIN')]
    #[Route(path: '/admin', name: 'admin')]
    public function index(): Response
    {
        // return parent::index();
        return $this->render(view: 'admin/index.html.twig');
    }

    public function configureDashboard(): Dashboard
    {
        return Dashboard::new()
            ->setTitle(title: '24unix Admin');
    }

    public function configureMenuItems(): iterable
    {
        yield MenuItem::linkToUrl(label: 'Homepage', icon: 'fa fa-home', url: $this->generateUrl(route: 'app_main'));
        yield MenuItem::linkToDashboard(label: 'Dashboard', icon: 'fa fa-dashboard');
        yield MenuItem::linkToCrud(label: 'Users', icon: 'fa fa-users', entityFqcn: User::class);
        yield MenuItem::linkToCrud(label: 'Projects', icon: 'fa fa-file-code-o', entityFqcn: Projects::class);
        yield MenuItem::linkToCrud(label: 'Pages', icon: 'fa fa-newspaper-o', entityFqcn: Pages::class);
        yield MenuItem::linkToCrud(label: 'Quotes', icon: 'fa fa-quote-left', entityFqcn: Quotes::class);
    }

    public function configureUserMenu(UserInterface $user): UserMenu
    {
        if (!$user instanceof User) {
            throw new Exception(message: 'Wrong User!');
        }

        return parent::configureUserMenu(user: $user)
            ->setAvatarUrl(url: 'uploads/avatars/' . $user->getAvatar());
    }

    public function configureActions(): Actions
    {
        return parent::configureActions()
            ->add(pageName: Crud::PAGE_INDEX, actionNameOrObject: Action::DETAIL);
    }
}
