<?php

declare(strict_types=1);

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

uses(RefreshDatabase::class);

it('uploads story editor images to object storage with hashed cdn urls', function (): void {
    Storage::fake('s3');

    config()->set('uploads.object_storage.disk', 's3');
    config()->set('cdn.files_url', 'https://cdn.skinbase.test');

    $creator = User::factory()->create();

    $response = $this->actingAs($creator)->postJson(route('api.story.upload-image'), [
        'image' => UploadedFile::fake()->image('cover.png', 1600, 900),
    ]);

    $response->assertOk();

    $mediumUrl = (string) $response->json('medium_url');
    $thumbnailUrl = (string) $response->json('thumbnail_url');
    $originalUrl = (string) $response->json('original_url');

    expect($mediumUrl)->toMatch('#^https://cdn\.skinbase\.test/stories/md/[a-f0-9]{2}/[a-f0-9]{2}/[a-f0-9]{64}\.webp$#');
    expect($thumbnailUrl)->toMatch('#^https://cdn\.skinbase\.test/stories/sm/[a-f0-9]{2}/[a-f0-9]{2}/[a-f0-9]{64}\.webp$#');
    expect($originalUrl)->toMatch('#^https://cdn\.skinbase\.test/stories/original/[a-f0-9]{2}/[a-f0-9]{2}/[a-f0-9]{64}\.(jpg|jpeg|png|webp)$#');

    Storage::disk('s3')->assertExists(ltrim(parse_url($mediumUrl, PHP_URL_PATH) ?: '', '/'));
    Storage::disk('s3')->assertExists(ltrim(parse_url($thumbnailUrl, PHP_URL_PATH) ?: '', '/'));
    Storage::disk('s3')->assertExists(ltrim(parse_url($originalUrl, PHP_URL_PATH) ?: '', '/'));
});