<?php

namespace App\Controller\Admin;

use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class UserCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return User::class;
    }

    public function configureFields(string $pageName): iterable
    {
        yield IdField::new(propertyName: 'id')
            ->onlyOnIndex();
        yield TextField::new(propertyName: 'firstName');
        yield TextField::new(propertyName: 'lastName');
        yield EmailField::new(propertyName: 'email');
        yield ImageField::new(propertyName: 'avatar')
            ->setBasePath(path: 'uploads/avatars')
            ->setUploadDir(uploadDirPath: 'public/uploads/avatars')
            ->setUploadedFileNamePattern(patternOrCallable: '[slug]-[timestamp].[extension]');
        $roles = ['ROLE_FOUNDER', 'ROLE_ADMIN', 'ROLE_MODERATOR', 'ROLE_USER'];
        yield ChoiceField::new(propertyName: 'roles')
            ->setChoices(choiceGenerator: array_combine(keys: $roles, values: $roles))
            ->allowMultipleChoices()
            ->renderExpanded()
            ->renderAsBadges();
    }
}
