<?php

declare(strict_types=1);

use App\Models\Collection;
use App\Models\User;
use App\Services\CollectionWorkflowService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;

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

it('rejects invalid workflow transitions', function (): void {
    $user = User::factory()->create();
    $collection = Collection::factory()->for($user)->create([
        'workflow_state' => Collection::WORKFLOW_DRAFT,
    ]);

    $service = app(CollectionWorkflowService::class);

    expect(fn () => $service->update($collection, [
        'workflow_state' => Collection::WORKFLOW_ARCHIVED,
    ], $user))->toThrow(ValidationException::class);
});

it('allows approved collections to become programmed', function (): void {
    $user = User::factory()->create(['role' => 'admin']);
    $collection = Collection::factory()->for($user)->create([
        'workflow_state' => Collection::WORKFLOW_APPROVED,
        'visibility' => Collection::VISIBILITY_PUBLIC,
        'lifecycle_state' => Collection::LIFECYCLE_PUBLISHED,
    ]);

    $service = app(CollectionWorkflowService::class);
    $updated = $service->update($collection, [
        'workflow_state' => Collection::WORKFLOW_PROGRAMMED,
        'program_key' => 'frontpage-hero',
    ], $user);

    expect($updated->workflow_state)->toBe(Collection::WORKFLOW_PROGRAMMED);
    expect($updated->program_key)->toBe('frontpage-hero');
});