<?php

declare(strict_types=1);

use App\Models\Artwork;
use App\Models\User;
use App\Models\World;
use App\Models\WorldRewardGrant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia;

uses(RefreshDatabase::class);

it('exposes world rewards on public profile pages', function (): void {
    $creator = User::factory()->create([
        'username' => 'profilerewards-' . strtolower(fake()->bothify('####')),
    ]);
    $worldOwner = User::factory()->create();
    $world = World::factory()->create([
        'title' => 'Spring Vibes 2026',
        'slug' => 'spring-vibes-2026-' . strtolower(fake()->bothify('####')),
        'status' => World::STATUS_PUBLISHED,
        'published_at' => now()->subDay(),
        'created_by_user_id' => $worldOwner->id,
    ]);
    $artwork = Artwork::factory()->for($creator)->create([
        'title' => 'Profile Reward Artwork',
        'slug' => 'profile-reward-artwork',
        'artwork_status' => 'published',
        'published_at' => now()->subDay(),
        'is_public' => true,
        'visibility' => Artwork::VISIBILITY_PUBLIC,
    ]);

    WorldRewardGrant::query()->create([
        'user_id' => $creator->id,
        'world_id' => $world->id,
        'artwork_id' => $artwork->id,
        'reward_type' => 'featured',
        'grant_source' => 'automatic',
        'granted_at' => now()->subHour(),
    ]);

    $this->get(route('profile.show', ['username' => strtolower((string) $creator->username)]))
        ->assertOk()
        ->assertInertia(fn (AssertableInertia $page) => $page
            ->component('Profile/ProfileShow')
            ->where('worldRewards.count', 1)
            ->where('worldRewards.items.0.badge_label', 'Spring Vibes 2026 Featured'));
});

it('prioritizes higher-signal world rewards ahead of participation on profile reward grids while keeping recents chronological', function (): void {
    $creator = User::factory()->create([
        'username' => 'profilepriority-' . strtolower(fake()->bothify('####')),
    ]);
    $worldOwner = User::factory()->create();

    $participantWorld = World::factory()->create([
        'title' => 'Retro Month 2026',
        'slug' => 'retro-month-2026-' . strtolower(fake()->bothify('####')),
        'status' => World::STATUS_PUBLISHED,
        'published_at' => now()->subDay(),
        'created_by_user_id' => $worldOwner->id,
    ]);

    $winnerWorld = World::factory()->create([
        'title' => 'Pixel Week 2026',
        'slug' => 'pixel-week-2026-' . strtolower(fake()->bothify('####')),
        'status' => World::STATUS_PUBLISHED,
        'published_at' => now()->subDay(),
        'created_by_user_id' => $worldOwner->id,
    ]);

    $participantArtwork = Artwork::factory()->for($creator)->create([
        'title' => 'Participant Artwork',
        'slug' => 'participant-artwork',
        'artwork_status' => 'published',
        'published_at' => now()->subDay(),
        'is_public' => true,
        'visibility' => Artwork::VISIBILITY_PUBLIC,
    ]);

    $winnerArtwork = Artwork::factory()->for($creator)->create([
        'title' => 'Winner Artwork',
        'slug' => 'winner-artwork',
        'artwork_status' => 'published',
        'published_at' => now()->subDay(),
        'is_public' => true,
        'visibility' => Artwork::VISIBILITY_PUBLIC,
    ]);

    WorldRewardGrant::query()->create([
        'user_id' => $creator->id,
        'world_id' => $participantWorld->id,
        'artwork_id' => $participantArtwork->id,
        'reward_type' => 'participant',
        'grant_source' => 'automatic',
        'granted_at' => now()->subMinutes(10),
    ]);

    WorldRewardGrant::query()->create([
        'user_id' => $creator->id,
        'world_id' => $winnerWorld->id,
        'artwork_id' => $winnerArtwork->id,
        'reward_type' => 'winner',
        'grant_source' => 'manual',
        'granted_at' => now()->subHour(),
    ]);

    $this->get(route('profile.show', ['username' => strtolower((string) $creator->username)]))
        ->assertOk()
        ->assertInertia(fn (AssertableInertia $page) => $page
            ->component('Profile/ProfileShow')
            ->where('worldRewards.count', 2)
            ->where('worldRewards.items.0.badge_label', 'Pixel Week 2026 Winner')
            ->where('worldRewards.items.1.badge_label', 'Retro Month 2026 Participant')
            ->where('worldRewards.recent.0.badge_label', 'Retro Month 2026 Participant'));
});

it('redirects public profile paths from stray skinbase subdomains to the canonical host', function (): void {
    config()->set('app.url', 'https://skinbase.org');

    User::factory()->create([
        'username' => 'ta2027',
    ]);

    $this->get('https://ffh84a.skinbase.org/@ta2027')
        ->assertRedirect('https://skinbase.org/@ta2027');
});