<?php

namespace App\Controller\Admin;

use App\Entity\Customer;
use App\Entity\Payment;
use App\Form\TicketType;
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\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TelephoneField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class CustomerCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Customer::class;
    }

    public function configureFields(string $pageName): iterable
    {
        yield TextField::new('firstname', 'Vorname');
        yield TextField::new('lastname', 'Nachname');
        yield EmailField::new('email', 'E-Mail');
        yield TelephoneField::new('phone', 'Telefon');
        yield AssociationField::new('payment', 'Total')
            ->setCrudController(PaymentCrudController::class)
            ->formatValue(fn(?Payment $payment) => ($payment?->getTotal() ?? 0.0) . ' €')
            ->hideOnIndex()
            ->hideOnForm();
        yield CollectionField::new('tickets')
            ->allowAdd()
            ->allowDelete()
            ->setEntryType(TicketType::class)
            ->setFormTypeOptions(['by_reference' => false])
            ->setTemplatePath('admin/customer_tickets.html.twig')
            ->hideOnIndex();
    }


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

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