<?php

use App\Models\DocumentTemplate;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

// for static analysis hints

it('shows index page', function () {
    $user = User::factory()->create();
    $user->givePermissionTo('manage-settings');
    $this->actingAs($user);

    $resp = $this->get(route('admin.document-templates.index'));
    $resp->assertSuccessful();
});

it('can upload a new document template version', function () {
    Storage::fake('public');
    $user = User::factory()->create();
    $user->givePermissionTo('manage-settings');
    $this->actingAs($user);

    $file = UploadedFile::fake()->create('test.docx', 12, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');

    $resp = $this->post(route('admin.document-templates.store'), [
        'name' => 'Test Predloga',
        'slug' => 'test-predloga',
        'file' => $file,
    ]);

    $resp->assertRedirect();
    expect(DocumentTemplate::where('slug', 'test-predloga')->exists())->toBeTrue();
});

it('can toggle active state', function () {
    $user = User::factory()->create();
    $user->givePermissionTo('manage-settings');
    $this->actingAs($user);

    $template = DocumentTemplate::factory()->create(['active' => true]);

    $resp = $this->post(route('admin.document-templates.toggle', $template));
    $resp->assertRedirect();
    $template->refresh();
    expect($template->active)->toBeFalse();
});

it('can view edit and show pages', function () {
    $user = User::factory()->create();
    $user->givePermissionTo('manage-settings');
    $this->actingAs($user);
    $template = DocumentTemplate::factory()->create();

    $this->get(route('admin.document-templates.show', $template))->assertSuccessful();
    $this->get(route('admin.document-templates.edit', $template))->assertSuccessful();
});
