<?php

namespace App\Controller\Admin;

use App\Entity\System;
use App\Enum\CelestialBodySizeEnum;
use App\Enum\StarTypeEnum;
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 SystemCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return System::class;
    }

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

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