<?php

namespace App\Controller\Admin;

use App\Entity\Game;
use App\Enum\GameStatusEnum;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class GameCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Game::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            IdField::new('id')->onlyOnIndex(),
            TextField::new('name'),
            ChoiceField::new('status')->setChoices($this->enumChoices(GameStatusEnum::class))->renderAsBadges(),
            DateTimeField::new('openedAt')->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;
    }
}
