<?php

declare(strict_types=1);

namespace App\Tests\Api;

use App\Tests\AbstractApiTestCase;

/**
 * @internal
 */
class PaginationTest extends AbstractApiTestCase
{
    public function testDefaultPagination(): void
    {
        for ($i = 1; $i <= 35; ++$i) {
            $this->createConstructeur("Constructeur {$i}");
        }

        $client = $this->createViewerClient();
        $client->request('GET', '/api/constructeurs');

        $this->assertResponseIsSuccessful();
        $data = $client->getResponse()->toArray();
        $this->assertSame(35, $data['totalItems']);
        $this->assertCount(30, $data['member']);
    }

    public function testCustomItemsPerPage(): void
    {
        for ($i = 1; $i <= 15; ++$i) {
            $this->createConstructeur("Constructeur {$i}");
        }

        $client = $this->createViewerClient();
        $client->request('GET', '/api/constructeurs?itemsPerPage=5');

        $this->assertResponseIsSuccessful();
        $data = $client->getResponse()->toArray();
        $this->assertCount(5, $data['member']);
        $this->assertSame(15, $data['totalItems']);
    }

    public function testPageNavigation(): void
    {
        for ($i = 1; $i <= 15; ++$i) {
            $this->createConstructeur("Constructeur {$i}");
        }

        $client = $this->createViewerClient();
        $client->request('GET', '/api/constructeurs?itemsPerPage=5&page=2');

        $this->assertResponseIsSuccessful();
        $data = $client->getResponse()->toArray();
        $this->assertCount(5, $data['member']);
    }

    public function testMaxItemsPerPageIsCapped(): void
    {
        for ($i = 1; $i <= 5; ++$i) {
            $this->createConstructeur("Constructeur {$i}");
        }

        $client = $this->createViewerClient();
        $client->request('GET', '/api/constructeurs?itemsPerPage=999');

        $this->assertResponseIsSuccessful();
        $data = $client->getResponse()->toArray();
        $this->assertCount(5, $data['member']);
    }
}
