<?php

declare(strict_types=1);

namespace Tests\Integration;

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

final class DownloadsTest extends QdbTestCase
{
    public function testExportTranslationsBundle(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $res = $this->api()->withToken($token, 'GET', 'translations', null, ['exportBundle' => '1']);
        $this->assertRawOk($res, 'application/json');
        $decoded = json_decode($res['body'], true);
        $this->assertIsArray($decoded);
    }

    public function testExportQuestionnairesBundle(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::SUPERVISOR_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $res = $this->api()->withToken($token, 'GET', 'export', null, ['bundle' => '1']);
        $this->assertRawOk($res, 'application/json');
        $decoded = json_decode($res['body'], true);
        $this->assertIsArray($decoded);
    }

    public function testExportAllZipAsAdmin(): void
    {
        if (!class_exists('ZipArchive')) {
            $this->markTestSkipped('php-zip extension not installed');
        }
        $this->submitFixtureQuestionnaire();
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $res = $this->api()->withToken($token, 'GET', 'export', null, ['exportAll' => '1']);
        $this->assertRawOk($res, 'application/zip');
        $this->assertStringStartsWith('PK', $res['body']);
    }
}
