<?php

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;

uses(RefreshDatabase::class);

test('macro import requires authentication', function () {
    $response = $this->post(route('settings.macros.import'), []);
    $response->assertRedirect(route('login'));
});

test('macro import returns json with stats on valid file', function () {
    $user = User::factory()->create();
    $response = $this->actingAs($user)
        ->post(route('settings.macros.import'), [
            'file' => new UploadedFile(base_path('tests/fixtures/macros-sample.bin'), 'macros.bin', null, null, true),
        ]);

    $response->assertStatus(200)
        ->assertJsonStructure(['stats' => ['new', 'updated', 'disabled', 're_enabled'], 'warnings']);
});

test('macro import returns 422 on invalid file', function () {
    $user = User::factory()->create();
    $response = $this->actingAs($user)
        ->post(route('settings.macros.import'), [
            'file' => UploadedFile::fake()->create('notamacro.bin', 1),
        ]);

    $response->assertStatus(422);
});

test('macro import without file returns validation error', function () {
    $user = User::factory()->create();
    $response = $this->actingAs($user)
        ->postJson(route('settings.macros.import'), []);

    $response->assertStatus(422);
});
