<?php

declare(strict_types=1);

namespace Tests\Integration;

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

final class ExportTest extends QdbTestCase
{
    public function testExportRequiresQuestionnaireId(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $res = $this->api()->withToken($token, 'GET', 'export');
        $this->assertApiError($res, 'MISSING_PARAM', 400);
    }

    public function testExportAllZipRequiresAdminRole(): void
    {
        if (!class_exists('ZipArchive')) {
            $this->markTestSkipped('php-zip extension not installed');
        }
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::SUPERVISOR_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $res = $this->api()->withToken($token, 'GET', 'export', null, ['exportAll' => '1']);
        $this->assertApiError($res, 'FORBIDDEN', 403);
    }

    public function testExportCsvForQuestionnaire(): void
    {
        $this->submitFixtureQuestionnaire();
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $f = $this->fixture();

        $res = $this->api()->withToken($token, 'GET', 'export', null, [
            'questionnaireID' => $f->questionnaireId,
        ]);
        $this->assertRawOk($res, 'text/csv');
        $this->assertStringContainsString($f->clientCode, $res['body']);
    }
}
