<?php

use App\Models\ContentType;
use App\Services\ContentTypeAssetService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

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

it('stores and deletes content type assets on the configured object storage disk', function () {
    Storage::fake('s3');
    Config::set('uploads.object_storage.disk', 's3');

    $contentType = ContentType::query()->create([
        'name' => 'Digital Art',
        'slug' => 'digital-art',
        'description' => 'Digital art uploads',
        'order' => 1,
    ]);

    $path = app(ContentTypeAssetService::class)->storeUploadedAsset(
        $contentType,
        UploadedFile::fake()->image('mascot.webp', 240, 320),
        'mascot'
    );

    expect($path)->toStartWith('content-types/' . $contentType->id . '/mascot-');
    expect(Storage::disk('s3')->exists($path))->toBeTrue();

    app(ContentTypeAssetService::class)->deleteIfManaged($path);

    expect(Storage::disk('s3')->exists($path))->toBeFalse();
});