<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

final class ExportProFileUploadTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        Storage::fake('local');
    }

    public function test_pro_datei_wird_als_prefix_akzeptiert(): void
    {
        $user = User::factory()->create();
        $file = UploadedFile::fake()->create('intro.pro', 10, 'application/octet-stream');

        $response = $this->actingAs($user)->post(route('settings.export-pro-files.store'), [
            'type' => 'prefix',
            'files' => [$file],
        ]);

        $response->assertStatus(201);
        $response->assertJsonPath('files.0.original_name', 'intro.pro');
    }

    public function test_probundle_datei_wird_als_prefix_akzeptiert(): void
    {
        $user = User::factory()->create();
        $file = UploadedFile::fake()->create('intro.probundle', 10, 'application/octet-stream');

        $response = $this->actingAs($user)->post(route('settings.export-pro-files.store'), [
            'type' => 'prefix',
            'files' => [$file],
        ]);

        $response->assertStatus(201);
        $response->assertJsonPath('files.0.original_name', 'intro.probundle');
    }

    public function test_pro_datei_wird_als_postfix_akzeptiert(): void
    {
        $user = User::factory()->create();
        $file = UploadedFile::fake()->create('outro.pro', 10, 'application/octet-stream');

        $response = $this->actingAs($user)->post(route('settings.export-pro-files.store'), [
            'type' => 'postfix',
            'files' => [$file],
        ]);

        $response->assertStatus(201);
        $response->assertJsonPath('files.0.original_name', 'outro.pro');
    }

    public function test_probundle_datei_wird_als_postfix_akzeptiert(): void
    {
        $user = User::factory()->create();
        $file = UploadedFile::fake()->create('outro.probundle', 10, 'application/octet-stream');

        $response = $this->actingAs($user)->post(route('settings.export-pro-files.store'), [
            'type' => 'postfix',
            'files' => [$file],
        ]);

        $response->assertStatus(201);
        $response->assertJsonPath('files.0.original_name', 'outro.probundle');
    }

    public function test_unerlaubte_dateiendung_wird_abgelehnt(): void
    {
        $user = User::factory()->create();
        $file = UploadedFile::fake()->create('document.txt', 10, 'text/plain');

        $response = $this->actingAs($user)->postJson(route('settings.export-pro-files.store'), [
            'type' => 'prefix',
            'files' => [$file],
        ]);

        $response->assertStatus(422);
    }

    public function test_unerlaubte_dateiendung_postfix_wird_abgelehnt(): void
    {
        $user = User::factory()->create();
        $file = UploadedFile::fake()->create('document.txt', 10, 'text/plain');

        $response = $this->actingAs($user)->postJson(route('settings.export-pro-files.store'), [
            'type' => 'postfix',
            'files' => [$file],
        ]);

        $response->assertStatus(422);
    }
}
