<?php

namespace App\Controller\Admin;

use App\Entity\Planet;
use App\Enum\CelestialBodySizeEnum;
use App\Enum\PlanetTypeEnum;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class PlanetCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Planet::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            IdField::new('id')->onlyOnIndex(),
            TextField::new('name'),
            ChoiceField::new('type')->setChoices($this->enumChoices(PlanetTypeEnum::class))->renderAsBadges(),
            IntegerField::new('position'),
            ChoiceField::new('size')->setChoices($this->enumChoices(CelestialBodySizeEnum::class))->renderAsBadges(),
        ];
    }

    /**
     * @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;
    }
}
