<?php

namespace App\Controller\Admin;

use App\Entity\Player;
use App\Enum\CareerEnum;
use App\Enum\PlayerStatusEnum;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class PlayerCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Player::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            IdField::new('id')->onlyOnIndex(),
            AssociationField::new('user'),
            AssociationField::new('game'),
            TextField::new('name'),
            ChoiceField::new('career')->setChoices($this->enumChoices(CareerEnum::class))->renderAsBadges(),
            DateTimeField::new('joinedAt')->setFormTypeOptions(['html5' => true, 'widget' => 'single_text'])->hideOnIndex(),
            ChoiceField::new('status')->setChoices($this->enumChoices(PlayerStatusEnum::class))->renderAsBadges(),
            IntegerField::new('score')->hideOnIndex(),
            DateTimeField::new('lastPlayedAt')->setFormTypeOptions(['html5' => true, 'widget' => 'single_text'])->hideOnIndex(),
        ];
    }

    /**
     * @param class-string $enumClass
     * @return array<string, string>
     */
    private function enumChoices(string $enumClass): array
    {
        $choices = [];
        foreach ($enumClass::cases() as $case) {
            $choices[$case->name] = $case;
        }
        return $choices;
    }
}
