<?php

namespace App\Tests\Feature\Manga;

use App\Entity\Manga;
use App\Tests\Feature\AbstractApiTestCase;
use Zenstruck\Foundry\Test\ResetDatabase;

class SearchMangaTest extends AbstractApiTestCase
{
    use ResetDatabase;

    public function testSearchMangaWithEmptyQuery(): void
    {
        // When
        $client = static::createClient();
        $response = $client->request('GET', '/api/manga-search', [
            'query' => [
                'q' => ''
            ]
        ]);

        // Then
        $this->assertResponseStatusCodeSame(400);
        $this->assertJsonContains([
            'hydra:title' => 'An error occurred',
            'hydra:description' => 'Le terme de recherche doit contenir au moins 3 caractères'
        ]);
    }

    public function testSearchMangaWithShortQuery(): void
    {
        // When
        $client = static::createClient();
        $response = $client->request('GET', '/api/manga-search', [
            'query' => [
                'q' => 'on'
            ]
        ]);

        // Then
        $this->assertResponseStatusCodeSame(400);
        $this->assertJsonContains([
            'hydra:title' => 'An error occurred',
            'hydra:description' => 'Le terme de recherche doit contenir au moins 3 caractères'
        ]);
    }

    public function testSearchMangaByTitle(): void
    {
        // Given
        $this->createManga('One Piece', 'one-piece');
        $this->createManga('Dragon Ball', 'dragon-ball');
        $this->createManga('One Punch Man', 'one-punch-man');

        // When
        $client = static::createClient();
        $response = $client->request('GET', '/api/manga-search', [
            'query' => [
                'q' => 'one'
            ]
        ]);

        // Then
        $this->assertResponseIsSuccessful();

        $data = $response->toArray();


        $this->assertCount(2, $data['items']['hydra:member']);
        $titles = array_map(fn($item) => $item['title'], $data['items']['hydra:member']);
        $this->assertContains('One Piece', $titles);
        $this->assertContains('One Punch Man', $titles);
    }

    public function testSearchMangaBySlug(): void
    {
        // Given
        $this->createManga('One Piece', 'one-piece');
        $this->createManga('Dragon Ball', 'dragon-ball');

        // When
        $client = static::createClient();
        $response = $client->request('GET', '/api/manga-search', [
            'query' => [
                'q' => 'dragon'
            ]
        ]);

        // Then
        $this->assertResponseIsSuccessful();
        $data = $response->toArray();

        $this->assertCount(1, $data['items']['hydra:member']);
        $this->assertEquals('Dragon Ball', $data['items']['hydra:member'][0]['title']);
        $this->assertEquals('dragon-ball', $data['items']['hydra:member'][0]['slug']);
    }

    // public function testSearchMangaWithPagination(): void
    // {
    //     // Given
    //     $this->createManga('One Piece', 'one-piece');
    //     $this->createManga('One Punch Man', 'one-punch-man');
    //     $this->createManga('One Outs', 'one-outs');

    //     // When
    //     $client = static::createClient();
    //     $response = $client->request('GET', '/api/mangas/search', [
    //         'query' => [
    //             'q' => 'one',
    //             'page' => 2,
    //             'limit' => 1
    //         ]
    //     ]);

    //     // Then
    //     $this->assertResponseIsSuccessful();
    //     $data = $response->toArray();

    //     $this->assertCount(1, $data['items']);
    //     $this->assertTrue($data['hasNextPage']);
    //     $this->assertTrue($data['hasPreviousPage']);
    // }

    private function createManga(string $title, string $slug): void
    {
        $manga = new Manga();
        $manga->setTitle($title)
            ->setSlug($slug)
            ->setDescription('Description test')
            ->setAuthor('Author test')
            ->setPublicationYear(2020)
            ->setGenres(['action'])
            ->setStatus('ongoing')
            ->setRating(4.5)
            ->setMonitored(false)
            ->setImageUrl('https://via.placeholder.com/150')
            ->setThumbnailUrl('https://via.placeholder.com/150')
            ->setCreatedAt(new \DateTimeImmutable('2020-01-01'));

        $this->entityManager->persist($manga);
        $this->entityManager->flush();
    }
}
