<?php

use App\Models\Service;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;

beforeEach(function () {
    Storage::fake('public');
    Queue::fake();
    $this->user = User::factory()->create();
    $this->actingAs($this->user);
    $this->service = Service::factory()->create();
});

/*
|--------------------------------------------------------------------------
| Key-Visual Upload
|--------------------------------------------------------------------------
*/

test('key visual upload with service scope sets column only', function () {
    $file = makeImageUpload('keyvisual.png', 800, 600);

    $response = $this->post(route('services.key-visual.store', $this->service), [
        'file' => $file,
        'scope' => 'service',
    ]);

    $response->assertRedirect();
    $response->assertSessionHas('success');

    $this->service->refresh();
    expect($this->service->key_visual_filename)->not->toBeNull();
    expect($this->service->key_visual_filename)->toStartWith('slides/');
    expect($this->service->key_visual_filename)->toEndWith('.jpg');
    expect(Storage::disk('public')->exists($this->service->key_visual_filename))->toBeTrue();

    expect(Setting::get('current_key_visual'))->toBeNull();
});

/*
|--------------------------------------------------------------------------
| Background Upload (default scope)
|--------------------------------------------------------------------------
*/

test('background upload with default scope sets column and global setting', function () {
    $file = makeImageUpload('background.png', 1920, 1080);

    $response = $this->post(route('services.background.store', $this->service), [
        'file' => $file,
        'scope' => 'default',
    ]);

    $response->assertRedirect();
    $response->assertSessionHas('success');

    $this->service->refresh();
    expect($this->service->background_filename)->not->toBeNull();
    expect($this->service->background_filename)->toStartWith('slides/');
    expect(Storage::disk('public')->exists($this->service->background_filename))->toBeTrue();

    expect(Setting::get('current_background'))->toBe($this->service->background_filename);
});

/*
|--------------------------------------------------------------------------
| Validation
|--------------------------------------------------------------------------
*/

test('key visual upload rejects non-image file with german error', function () {
    $file = UploadedFile::fake()->create('notes.txt', 10, 'text/plain');

    $response = $this->postJson(route('services.key-visual.store', $this->service), [
        'file' => $file,
        'scope' => 'service',
    ]);

    $response->assertStatus(422);
    $response->assertJsonValidationErrors(['file']);
    expect($response->json('errors.file.0'))->toContain('Bild');

    $this->service->refresh();
    expect($this->service->key_visual_filename)->toBeNull();
});

test('background upload rejects invalid scope', function () {
    $file = makeImageUpload('background.png', 800, 600);

    $response = $this->postJson(route('services.background.store', $this->service), [
        'file' => $file,
        'scope' => 'bogus',
    ]);

    $response->assertStatus(422);
    $response->assertJsonValidationErrors(['scope']);
});

test('key visual upload does not delete previous file', function () {
    $first = makeImageUpload('first.png', 800, 600);
    $this->post(route('services.key-visual.store', $this->service), [
        'file' => $first,
        'scope' => 'service',
    ]);
    $this->service->refresh();
    $oldFilename = $this->service->key_visual_filename;

    $second = makeImageUpload('second.png', 800, 600);
    $this->post(route('services.key-visual.store', $this->service), [
        'file' => $second,
        'scope' => 'service',
    ]);
    $this->service->refresh();

    expect($this->service->key_visual_filename)->not->toBe($oldFilename);
    expect(Storage::disk('public')->exists($oldFilename))->toBeTrue();
});

/*
|--------------------------------------------------------------------------
| Auth
|--------------------------------------------------------------------------
*/

test('key visual upload requires authentication', function () {
    auth()->logout();
    $file = makeImageUpload('keyvisual.png', 800, 600);

    $response = $this->post(route('services.key-visual.store', $this->service), [
        'file' => $file,
        'scope' => 'service',
    ]);

    $response->assertRedirect(route('login'));
});

function makeImageUpload(string $name = 'test.png', int $w = 800, int $h = 600): UploadedFile
{
    $path = tempnam(sys_get_temp_dir(), 'cts-svc-img-');
    if ($path === false) {
        throw new RuntimeException('Temp-Datei konnte nicht erstellt werden.');
    }

    $image = imagecreatetruecolor($w, $h);
    if ($image === false) {
        throw new RuntimeException('Bild konnte nicht erstellt werden.');
    }

    $blue = imagecolorallocate($image, 0, 0, 255);
    imagefill($image, 0, 0, $blue);
    imagepng($image, $path);
    imagedestroy($image);

    return new UploadedFile($path, $name, 'image/png', null, true);
}
