<?php

declare(strict_types=1);

use App\Models\Artwork;
use App\Models\User;
use Carbon\Carbon;
use Inertia\Testing\AssertableInertia;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;

it('publishes overdue scheduled artwork when opening the studio edit page', function (): void {
    Carbon::setTestNow('2026-04-16 08:00:00');

    try {
        $user = User::factory()->create();
        $artwork = Artwork::factory()->private()->create([
            'user_id' => $user->id,
            'visibility' => Artwork::VISIBILITY_PUBLIC,
            'is_approved' => true,
            'is_public' => false,
            'artwork_status' => 'scheduled',
            'publish_at' => now()->subHour(),
            'published_at' => null,
            'artwork_timezone' => 'Europe/Ljubljana',
        ]);

        actingAs($user);

        get('/studio/artworks/' . $artwork->id . '/edit')
            ->assertOk()
            ->assertInertia(fn (AssertableInertia $page) => $page
                ->component('Studio/StudioArtworkEdit')
                ->where('artwork.artwork_status', 'published')
                ->where('artwork.publish_mode', 'now')
                ->where('artwork.publish_at', null)
                ->where('artwork.is_public', true));

        $artwork->refresh();

        expect($artwork->artwork_status)->toBe('published')
            ->and($artwork->is_public)->toBeTrue()
            ->and($artwork->publish_at)->toBeNull()
            ->and($artwork->artwork_timezone)->toBeNull()
            ->and($artwork->published_at)->not->toBeNull();
    } finally {
        Carbon::setTestNow();
    }
});