<?php

declare(strict_types=1);

namespace Tests\Integration;

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

final class QuestionnaireEditingTest extends QdbTestCase
{
    public function testAdminCreatesQuestion(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $f = $this->fixture();
        $res = $this->api()->withToken($token, 'POST', 'questions', [
            'questionnaireID' => $f->questionnaireId,
            'questionKey'     => 'extra_q',
            'defaultText'     => 'Extra question text',
            'type'            => 'free_text',
            'isRequired'      => 0,
            'configJson'      => '{}',
        ]);
        $this->assertApiOk($res);
        $this->assertSame('extra_q', $res['data']['question']['questionKey'] ?? '');
    }

    public function testAdminCreatesAnswerOption(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $f = $this->fixture();
        $res = $this->api()->withToken($token, 'POST', 'answer_options', [
            'questionID'  => $f->questionId,
            'optionKey'   => 'no_option',
            'defaultText' => 'No',
            'points'      => 0,
        ]);
        $this->assertApiOk($res);
        $this->assertSame('no_option', $res['data']['answerOption']['optionKey'] ?? '');
    }

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