<?php

declare(strict_types=1);

namespace Tests\Integration;

use Tests\Support\DatabaseSeeder;
use Tests\Support\QdbTestCase;

final class ClientsQuestionnairesTest extends QdbTestCase
{
    public function testSupervisorSeesClients(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::SUPERVISOR_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $res = $this->api()->withToken($token, 'GET', 'clients');
        $this->assertApiOk($res);
        $codes = array_column($res['data']['clients'], 'clientCode');
        $this->assertContains($this->fixture()->clientCode, $codes);
    }

    public function testAdminCreatesClient(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $code = 'CLIENT-NEW-' . bin2hex(random_bytes(2));
        $res = $this->api()->withToken($token, 'POST', 'clients', [
            'clientCode' => $code,
            'coachID'    => $this->fixture()->coachId,
        ]);
        $this->assertApiOk($res);
        $this->assertSame($code, $res['data']['clientCode']);
    }

    public function testQuestionnairesList(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $res = $this->api()->withToken($token, 'GET', 'questionnaires');
        $this->assertApiOk($res);
        $ids = array_column($res['data']['questionnaires'], 'questionnaireID');
        $this->assertContains($this->fixture()->questionnaireId, $ids);
    }

    public function testQuestionsRequireQuestionnaireId(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $res = $this->api()->withToken($token, 'GET', 'questions');
        $this->assertApiError($res, 'BAD_REQUEST', 400);
    }
}
