<?php

namespace Tests\Feature\Manga;

use App\Entity\Chapter;
use App\Entity\Manga;
use Zenstruck\Foundry\Test\ResetDatabase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class EditMultipleChaptersTest extends WebTestCase
{
    use ResetDatabase;

    public function test_it_edits_multiple_chapters(): void
    {
        // Given
        $client = static::createClient();

        // Créer un manga et des chapitres de test
        $entityManager = static::getContainer()->get('doctrine')->getManager();

        $manga = new Manga();
        $manga->setTitle('Test Manga');
        $manga->setSlug('test-manga');
        $manga->setMonitored(true);
        $entityManager->persist($manga);

        $chapter1 = new Chapter();
        $chapter1->setManga($manga);
        $chapter1->setNumber(1.0);
        $chapter1->setTitle('Old Title 1');
        $chapter1->setVolume(1);
        $entityManager->persist($chapter1);

        $chapter2 = new Chapter();
        $chapter2->setManga($manga);
        $chapter2->setNumber(2.0);
        $chapter2->setTitle('Old Title 2');
        $chapter2->setVolume(1);
        $entityManager->persist($chapter2);

        $entityManager->flush();

        $data = [
            'chapters' => [
                [
                    'id' => (string) $chapter1->getId(),
                    'title' => 'New Title 1',
                    'volume' => 2
                ],
                [
                    'id' => (string) $chapter2->getId(),
                    'title' => null,
                    'volume' => 3
                ]
            ]
        ];

        // When
        $client->request('POST', '/api/chapters/batch-edit', [], [], [
            'CONTENT_TYPE' => 'application/json'
        ], json_encode($data));

        // Then
        $this->assertResponseIsSuccessful();

        // Vérifier que les chapitres ont été mis à jour
        $entityManager->clear();

        $updatedChapter1 = $entityManager->find(Chapter::class, $chapter1->getId());
        $this->assertEquals('New Title 1', $updatedChapter1->getTitle());
        $this->assertEquals(2, $updatedChapter1->getVolume());

        $updatedChapter2 = $entityManager->find(Chapter::class, $chapter2->getId());
        $this->assertEquals('Old Title 2', $updatedChapter2->getTitle()); // Non modifié
        $this->assertEquals(3, $updatedChapter2->getVolume());
    }

    public function test_it_returns_404_when_chapter_not_found(): void
    {
        // Given
        $client = static::createClient();

        $data = [
            'chapters' => [
                [
                    'id' => '999',
                    'title' => 'New Title',
                    'volume' => 1
                ]
            ]
        ];

        // When
        $client->request('POST', '/api/chapters/batch-edit', [], [], [
            'CONTENT_TYPE' => 'application/json'
        ], json_encode($data));

        // Then
        $this->assertResponseStatusCodeSame(404);
    }

        public function test_it_validates_required_fields(): void
    {
        // Given
        $client = static::createClient();

        $data = [
            'chapters' => [
                [
                    'title' => 'New Title',
                    'volume' => 1
                    // id manquant
                ]
            ]
        ];

        // When
        $client->request('POST', '/api/chapters/batch-edit', [], [], [
            'CONTENT_TYPE' => 'application/json'
        ], json_encode($data));

        // Then
        $this->assertResponseStatusCodeSame(500); // Erreur interne due à la validation manuelle
    }
}
