<?php

namespace Tests\Feature;

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

final class ExportProFileKeywordTest extends TestCase
{
    use RefreshDatabase;

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

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

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

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

        $record = ExportProFile::query()->firstOrFail();
        $this->assertSame('keyword', $record->type);
        $this->assertSame('Abendmahl', $record->keyword);
        $this->assertTrue(Storage::disk('local')->exists($record->stored_path));
        $this->assertStringContainsString('export-pro-files/keyword', $record->stored_path);
    }

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

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

        $response->assertStatus(422);
        $response->assertJsonValidationErrors('keyword');
        $this->assertSame(0, ExportProFile::query()->count());
    }

    public function test_batch_keyword_wird_allen_dateien_zugewiesen(): void
    {
        $user = User::factory()->create();
        $fileA = UploadedFile::fake()->create('a.pro', 10, 'application/octet-stream');
        $fileB = UploadedFile::fake()->create('b.pro', 10, 'application/octet-stream');

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

        $response->assertStatus(201);
        $this->assertSame(2, ExportProFile::keyword()->count());
        $this->assertSame(['Taufe', 'Taufe'], ExportProFile::keyword()->orderBy('order')->pluck('keyword')->all());
    }

    public function test_update_keyword_aktualisiert_schluesselwort(): void
    {
        $user = User::factory()->create();
        $record = ExportProFile::create([
            'type' => 'keyword',
            'keyword' => 'Alt',
            'original_name' => 'file.pro',
            'stored_path' => 'export-pro-files/keyword/file.pro',
            'order' => 1,
        ]);

        $response = $this->actingAs($user)->patchJson(
            route('settings.export-pro-files.update-keyword', $record),
            ['keyword' => 'Neu'],
        );

        $response->assertOk();
        $this->assertSame('Neu', $record->fresh()->keyword);
    }

    public function test_update_keyword_erfordert_schluesselwort(): void
    {
        $user = User::factory()->create();
        $record = ExportProFile::create([
            'type' => 'keyword',
            'keyword' => 'Alt',
            'original_name' => 'file.pro',
            'stored_path' => 'export-pro-files/keyword/file.pro',
            'order' => 1,
        ]);

        $response = $this->actingAs($user)->patchJson(
            route('settings.export-pro-files.update-keyword', $record),
            ['keyword' => ''],
        );

        $response->assertStatus(422);
        $response->assertJsonValidationErrors('keyword');
    }

    public function test_update_keyword_fuer_nicht_keyword_datei_wird_abgelehnt(): void
    {
        $user = User::factory()->create();
        $record = ExportProFile::create([
            'type' => 'prefix',
            'keyword' => null,
            'original_name' => 'intro.pro',
            'stored_path' => 'export-pro-files/prefix/intro.pro',
            'order' => 1,
        ]);

        $response = $this->actingAs($user)->patchJson(
            route('settings.export-pro-files.update-keyword', $record),
            ['keyword' => 'Abendmahl'],
        );

        $response->assertStatus(422);
        $this->assertNull($record->fresh()->keyword);
    }

    public function test_settings_index_enthaelt_keyword_dateien(): void
    {
        $user = User::factory()->create();
        ExportProFile::create([
            'type' => 'keyword',
            'keyword' => 'Abendmahl',
            'original_name' => 'kommunion.pro',
            'stored_path' => 'export-pro-files/keyword/kommunion.pro',
            'order' => 1,
        ]);

        $response = $this->actingAs($user)
            ->withoutVite()
            ->get(route('settings.index'));

        $response->assertInertia(
            fn ($page) => $page
                ->component('Settings')
                ->has('export_pro_files.keyword', 1)
                ->where('export_pro_files.keyword.0.keyword', 'Abendmahl')
                ->where('export_pro_files.keyword.0.original_name', 'kommunion.pro')
        );
    }
}
