<?php

use App\Models\User;
use App\Uploads\Services\PublishService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Tests\TestCase;

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

function createCategoryForPublishTests(): int
{
    $contentTypeId = DB::table('content_types')->insertGetId([
        'name' => 'Skins',
        'slug' => 'skins-' . Str::lower(Str::random(6)),
        'description' => null,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    return DB::table('categories')->insertGetId([
        'content_type_id' => $contentTypeId,
        'parent_id' => null,
        'name' => 'Winamp',
        'slug' => 'winamp-' . Str::lower(Str::random(6)),
        'description' => null,
        'image' => null,
        'is_active' => true,
        'sort_order' => 0,
        'created_at' => now(),
        'updated_at' => now(),
    ]);
}

it('rejects publish when user is not owner', function () {
    Storage::fake('local');

    $owner = User::factory()->create();
    $other = User::factory()->create();
    $categoryId = createCategoryForPublishTests();
    $uploadId = (string) Str::uuid();

    DB::table('uploads')->insert([
        'id' => $uploadId,
        'user_id' => $owner->id,
        'type' => 'image',
        'status' => 'draft',
        'moderation_status' => 'approved',
        'title' => 'City Lights',
        'category_id' => $categoryId,
        'is_scanned' => true,
        'has_tags' => true,
        'preview_path' => "tmp/drafts/{$uploadId}/preview.webp",
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $service = app(PublishService::class);

    expect(fn () => $service->publish($uploadId, $other))
        ->toThrow(RuntimeException::class, 'You do not own this upload.');
});

it('rejects archive publish without screenshots', function () {
    Storage::fake('local');

    $owner = User::factory()->create();
    $categoryId = createCategoryForPublishTests();
    $uploadId = (string) Str::uuid();

    DB::table('uploads')->insert([
        'id' => $uploadId,
        'user_id' => $owner->id,
        'type' => 'archive',
        'status' => 'draft',
        'moderation_status' => 'approved',
        'title' => 'Skin Pack',
        'category_id' => $categoryId,
        'is_scanned' => true,
        'has_tags' => true,
        'preview_path' => "tmp/drafts/{$uploadId}/preview.webp",
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    DB::table('upload_files')->insert([
        'upload_id' => $uploadId,
        'path' => "tmp/drafts/{$uploadId}/main/pack.zip",
        'type' => 'main',
        'hash' => 'aabbccddeeff0011',
        'size' => 1024,
        'mime' => 'application/zip',
        'created_at' => now(),
    ]);

    $service = app(PublishService::class);

    expect(fn () => $service->publish($uploadId, $owner))
        ->toThrow(RuntimeException::class, 'Archive uploads require at least one screenshot.');
});
