<?php

namespace App\Tests\Feature\Manga;

use App\Domain\Manga\Domain\Contract\Service\ImageProcessorInterface;
use App\Tests\Feature\AbstractApiTestCase;
use Zenstruck\Foundry\Test\ResetDatabase;

class CreateMangaDirectlyTest extends AbstractApiTestCase
{
    use ResetDatabase;

    public function testCreateMangaDirectly(): void
    {
        // When
        $client = static::createClient();
        $response = $client->request('POST', '/api/mangas/create', [
            'json' => [
                'title' => 'One Piece',
                'slug' => 'one-piece',
                'description' => 'Test description',
                'author' => 'Eiichiro Oda',
                'publicationYear' => 1997,
                'genres' => ['action', 'adventure'],
                'status' => 'ongoing',
                'externalId' => 'external-123',
                'imageUrl' => 'http://example.com/image.jpg',
                'rating' => 4.5
            ]
        ]);

        // Then
        $this->assertResponseIsSuccessful();
        
        // Verify the manga was created in database
        $entityManager = static::getContainer()->get('doctrine')->getManager();
        $manga = $entityManager->getRepository(\App\Entity\Manga::class)->findOneBy(['slug' => 'one-piece']);
        
        $this->assertNotNull($manga);
        $this->assertEquals('One Piece', $manga->getTitle());
        $this->assertEquals('Test description', $manga->getDescription());
        $this->assertEquals('Eiichiro Oda', $manga->getAuthor());
        $this->assertEquals(1997, $manga->getPublicationYear());
        $this->assertEquals(['action', 'adventure'], $manga->getGenres());
        $this->assertEquals('ongoing', $manga->getStatus());
        $this->assertEquals('external-123', $manga->getExternalId());
        $this->assertEquals('/images/full/test-image.jpg', $manga->getImageUrl());
        $this->assertEquals('/images/thumbnails/test-image.jpg', $manga->getThumbnailUrl());
        $this->assertEquals(4.5, $manga->getRating());
    }

    public function testCreateMangaWithoutImage(): void
    {
        // When
        $client = static::createClient();
        $response = $client->request('POST', '/api/mangas/create', [
            'json' => [
                'title' => 'One Piece',
                'slug' => 'one-piece',
                'description' => 'Test description',
                'author' => 'Eiichiro Oda',
                'publicationYear' => 1997,
                'genres' => ['action', 'adventure'],
                'status' => 'ongoing',
                'externalId' => 'external-123',
                'imageUrl' => null,
                'rating' => 4.5
            ]
        ]);

        // Then
        $this->assertResponseIsSuccessful();
        
        $entityManager = static::getContainer()->get('doctrine')->getManager();
        $manga = $entityManager->getRepository(\App\Entity\Manga::class)->findOneBy(['slug' => 'one-piece']);
        
        $this->assertNotNull($manga);
        $this->assertNull($manga->getImageUrl());
        $this->assertNull($manga->getThumbnailUrl());
    }

    public function testCreateMangaWithInvalidData(): void
    {
        // When
        $client = static::createClient();
        $response = $client->request('POST', '/api/mangas/create', [
            'json' => [
                'title' => '', // Invalid: empty title
                'publicationYear' => 'invalid', // Invalid: not a number
            ]
        ]);

        // Then
        $this->assertResponseStatusCodeSame(400);
        $this->assertJsonContains([
            'hydra:title' => 'An error occurred',
            'hydra:description' => 'The type of the "publicationYear" attribute must be "int", "string" given.'
        ]);
    }

    public function testCreateMangaWithInvalidYear(): void
    {
        // When
        $client = static::createClient();
        $response = $client->request('POST', '/api/mangas/create', [
            'json' => [
                'title' => 'One Piece',
                'slug' => 'one-piece',
                'description' => 'Test description',
                'author' => 'Eiichiro Oda',
                'publicationYear' => 2200, // Année invalide > 2100
                'genres' => ['action', 'adventure'],
                'status' => 'ongoing',
                'externalId' => 'external-123',
                'imageUrl' => null,
                'rating' => 4.5
            ]
        ]);

        // Then
        $this->assertResponseStatusCodeSame(422);
        $this->assertJsonContains([
            'hydra:title' => 'An error occurred',
            'hydra:description' => 'publicationYear: L\'année de publication doit être comprise entre 1900 et 2100',
            'title' => 'An error occurred'
        ]);
    }
} 