<?php

use App\Services\FileConversionService;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

beforeEach(function () {
    Storage::fake('public');
});

test('cover conversion fills 1920x1080 without black bars', function () {
    $service = app(FileConversionService::class);
    $file = makePngUploadForFileConversionService('cover.png', 1200, 900);

    $result = $service->convertImageCover($file);

    expect($result)->toHaveKeys(['filename', 'thumbnail', 'warnings', 'fullCover']);
    expect($result['fullCover'])->toBeTrue();
    expect(Storage::disk('public')->exists($result['filename']))->toBeTrue();
    expect(Storage::disk('public')->exists($result['thumbnail']))->toBeTrue();

    $mainPath = Storage::disk('public')->path($result['filename']);
    [$width, $height] = getimagesize($mainPath);

    expect($width)->toBe(1920);
    expect($height)->toBe(1080);

    $image = imagecreatefromjpeg($mainPath);
    expect($image)->not->toBeFalse();

    foreach ([[0, 0], [1919, 0], [0, 1079], [1919, 1079]] as [$x, $y]) {
        $corner = imagecolorsforindex($image, imagecolorat($image, $x, $y));
        expect($corner['red'] + $corner['green'] + $corner['blue'])->toBeGreaterThan(20);
    }
});

test('contain conversion keeps black bars and fullCover false', function () {
    $service = app(FileConversionService::class);
    $file = makePngUploadForFileConversionService('contain.png', 1200, 900);

    $result = $service->convertImage($file);

    expect($result)->toHaveKeys(['filename', 'thumbnail', 'warnings', 'fullCover']);
    expect($result['fullCover'])->toBeFalse();

    $mainPath = Storage::disk('public')->path($result['filename']);
    [$width, $height] = getimagesize($mainPath);

    expect($width)->toBe(1920);
    expect($height)->toBe(1080);

    $image = imagecreatefromjpeg($mainPath);
    expect($image)->not->toBeFalse();

    foreach ([[0, 0], [1919, 0], [0, 1079], [1919, 1079]] as [$x, $y]) {
        $corner = imagecolorsforindex($image, imagecolorat($image, $x, $y));
        expect($corner['red'] + $corner['green'] + $corner['blue'])->toBeLessThan(20);
    }
});

test('cover conversion upscales small sources with German quality warning', function () {
    $service = app(FileConversionService::class);
    $file = makePngUploadForFileConversionService('small-cover.png', 800, 600);

    $result = $service->convertImageCover($file);

    $mainPath = Storage::disk('public')->path($result['filename']);
    [$width, $height] = getimagesize($mainPath);

    expect($width)->toBe(1920);
    expect($height)->toBe(1080);
    expect($result['warnings'])->not->toBeEmpty();
    expect(implode(' ', $result['warnings']))->toContain('kleiner als 1920×1080');
    expect(implode(' ', $result['warnings']))->toContain('hochskaliert');
});

function makePngUploadForFileConversionService(string $name, int $width, int $height): UploadedFile
{
    $path = tempnam(sys_get_temp_dir(), 'cts-cover-');
    if ($path === false) {
        throw new RuntimeException('Temp-Datei konnte nicht erstellt werden.');
    }

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

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

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