<?php

declare(strict_types=1);

use App\Models\Artwork;
use App\Models\User;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\postJson;

it('creates upload drafts as private artworks', function (): void {
    $user = User::factory()->create();

    actingAs($user);

    $response = postJson('/api/artworks', [
        'title' => 'Upload draft test',
        'description' => 'Draft body',
        'is_mature' => false,
    ]);

    $response->assertCreated()
        ->assertJsonPath('status', 'draft');

    $artworkId = (int) $response->json('artwork_id');
    $artwork = Artwork::query()->findOrFail($artworkId);

    expect($artwork->visibility)->toBe(Artwork::VISIBILITY_PRIVATE)
        ->and($artwork->is_public)->toBeFalse()
        ->and($artwork->artwork_status)->toBe('draft')
        ->and($artwork->published_at)->toBeNull();
});

it('rejects upload drafts with raw html in the description', function (): void {
    $user = User::factory()->create();

    actingAs($user);

    postJson('/api/artworks', [
        'title' => 'Upload draft test',
        'description' => '<img src="https://spam.example/test.jpg" alt="">',
        'is_mature' => false,
    ])->assertStatus(422)
        ->assertJsonValidationErrors(['description']);
});