<?php

namespace App\Controller\Api;

use App\DTO\TaskSchemaRequest;
use App\Entity\TaskSchema;
use App\Repository\TaskSchemaRepository;
use App\Service\TaskSchemaManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload as Payload;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/api/task-schemas')]
class TaskSchemaController extends AbstractController
{
    public function __construct(
        private readonly TaskSchemaRepository $repo,
        private readonly TaskSchemaManager $manager,
    ) {
    }

    #[Route('', methods: ['GET'])]
    public function index(): JsonResponse
    {
        $schemas = $this->repo->allSchemas();

        return $this->json($schemas, 200, [], ['groups' => ['task_schema:read']]);
    }

    #[Route('/{id}', methods: ['GET'], requirements: ['id' => '\d+'])]
    public function show(TaskSchema $schema): JsonResponse
    {
        return $this->json($schema, 200, [], ['groups' => ['task_schema:read']]);
    }

    #[Route('', methods: ['POST'])]
    public function create(#[Payload] TaskSchemaRequest $dto): JsonResponse
    {
        $this->manager->create($dto);

        return new JsonResponse(null, 201);
    }

    #[Route('/{id}', methods: ['PUT'], requirements: ['id' => '\d+'])]
    public function update(TaskSchema $schema, #[Payload] TaskSchemaRequest $dto): JsonResponse
    {
        $this->manager->update($schema, $dto);

        return $this->json($schema, 200, [], ['groups' => ['task_schema:read']]);
    }

    #[Route('/{id}', methods: ['DELETE'], requirements: ['id' => '\d+'])]
    public function delete(TaskSchema $schema): JsonResponse
    {
        $this->manager->delete($schema);

        return new JsonResponse(null, 204);
    }
}
