<?php

declare(strict_types=1);

use App\Services\Upload\PreviewService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Tests\TestCase;

uses(TestCase::class, RefreshDatabase::class);

it('generates a smart square thumb during preview creation', function () {
    Storage::fake('local');

    $uploadId = (string) Str::uuid();
    $file = UploadedFile::fake()->image('preview-source.jpg', 1200, 800);
    $path = $file->storeAs("tmp/drafts/{$uploadId}/main", 'preview-source.jpg', 'local');

    $result = app(PreviewService::class)->generateFromImage($uploadId, $path);

    expect(Storage::disk('local')->exists($result['preview_path']))->toBeTrue()
        ->and(Storage::disk('local')->exists($result['thumb_path']))->toBeTrue();

    $thumbSize = getimagesizefromstring(Storage::disk('local')->get($result['thumb_path']));

    expect($thumbSize[0] ?? null)->toBe(320)
        ->and($thumbSize[1] ?? null)->toBe(320);
});