<?php

namespace App\Controller\DataTable;

use App\DataTable\SpaceMissionSelectableTableType;
use App\Entity\SpaceMission;
use App\Form\SpaceMissionBulkType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Umbrella\AdminBundle\Lib\Controller\AdminController;
use Umbrella\AdminBundle\Lib\DataTable\Utils\DataTableActionState;

#[Route('/datatable/selectable')]
class DataTableSelectableController extends AdminController
{
    #[Route('')]
    public function index(Request $request): Response
    {
        $table = $this->createTable(SpaceMissionSelectableTableType::class);
        $table->handleRequest($request);

        if ($table->isCallback()) {
            return $table->getCallbackResponse();
        }

        return $this->render('datatable/selectable/index.html.twig', [
            'table' => $table,
        ]);
    }

    /**
     * @return SpaceMission[]
     */
    private function getBulkMission(Request $request): array
    {
        $state = DataTableActionState::createFromRequest($request);

        return $this->createTable(SpaceMissionSelectableTableType::class)
            ->getAdapterQueryBuilder()
            ->andWhere('e.id IN (:ids)')
            ->setParameter('ids', $state->getSelectedIds())
            ->getQuery()
            ->getResult();
    }

    #[Route('/edit')]
    public function edit(Request $request): Response
    {
        $form = $this
            ->createForm(SpaceMissionBulkType::class)
            ->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $missions = $this->getBulkMission($request);
            $data = $form->getData();

            foreach ($missions as $mission) {
                if ($data['rocketStatus']) {
                    $mission->rocketStatus = $data['rocketStatus'];
                }

                if ($data['missionStatus']) {
                    $mission->missionStatus = $data['missionStatus'];
                }
            }

            $this->em()->flush();

            return $this->js()
                ->closeModal()
                ->toastSuccess(sprintf('%d row.s updated', count($missions)))
                ->unselectTable()
                ->reloadTable();
        }

        return $this->js()
            ->modal('@UmbrellaAdmin/lib/modal/form.html.twig', [
                'title' => 'Bulk edition',
                'form' => $form->createView()
            ]);
    }

    #[Route('/delete')]
    public function delete(Request $request): Response
    {
        $missions = $this->getBulkMission($request);
        foreach ($missions as $mission) {
            $this->em()->remove($mission);
        }
        $this->em()->flush();

        return $this->js()
            ->toastSuccess(sprintf('%d row.s deleted', count($missions)))
            ->unselectTable()
            ->reloadTable();
    }
}
