<?php

use App\Http\Controllers\ContentRouterController;
use App\Models\Artwork;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\View\View;

uses(RefreshDatabase::class);

it('handles model-bound artwork parameter without 404 fallback', function () {
    $artwork = Artwork::factory()->create([
        'slug' => 'bound-model-artwork',
        'is_public' => true,
        'is_approved' => true,
        'published_at' => now()->subMinute(),
    ]);

    $request = Request::create('/photography/abstract/bound-model-artwork', 'GET');

    $response = app(ContentRouterController::class)->handle(
        $request,
        'photography',
        'abstract',
        $artwork
    );

    expect($response)->toBeInstanceOf(View::class);
    expect($response->name())->toBe('artworks.show');
    expect($response->getData()['artwork']->id)->toBe($artwork->id);
});

it('binds routed artwork model and renders published artwork page', function () {
    $artwork = Artwork::factory()->create([
        'title' => 'Routed Binding Artwork',
        'slug' => 'routed-binding-artwork',
        'is_public' => true,
        'is_approved' => true,
        'published_at' => now()->subMinute(),
    ]);

    $url = route('artworks.show', [
        'contentTypeSlug' => 'photography',
        'categoryPath' => 'abstract',
        'artwork' => $artwork->slug,
    ]);

    $matchedRoute = app('router')->getRoutes()->match(Request::create($url, 'GET'));
    app('router')->substituteBindings($matchedRoute);
    expect($matchedRoute->parameter('artwork'))->toBeInstanceOf(Artwork::class);
    expect($matchedRoute->parameter('artwork')->id)->toBe($artwork->id);

    $this->get($url)
        ->assertOk()
        ->assertSee('Routed Binding Artwork');
});
