<?php

namespace App\Controller\Admin;

use App\Entity\Payment;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;

class PaymentCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Payment::class;
    }

    public function configureFields(string $pageName): iterable
    {
        yield AssociationField::new('customer', 'Kunde')
            ->setCrudController(CustomerCrudController::class)
            ->formatValue(fn($value, $payment) => $payment->getCustomer()->getEmail());
        yield BooleanField::new('completed', 'Bezahlt')->renderAsSwitch(false);
        yield NumberField::new('donation', 'Spende')
            ->formatValue(fn (?float $donation) => ($donation??'0').'€');
    }

    public function configureActions(Actions $actions): Actions
    {
        return $actions
            ->add(Crud::PAGE_INDEX, Action::DETAIL)
            ->disable(Action::DELETE)
            ->disable(Action::NEW)
            ->setPermission(Action::EDIT, 'ROLE_SUPER_ADMIN');
    }

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->setPageTitle(Crud::PAGE_INDEX, 'Zahlungen')
            ->setPageTitle(Crud::PAGE_DETAIL, 'Zahlung')
            ->showEntityActionsInlined();
    }
}
