<?php

use App\Models\Artwork;
use App\Models\Category;
use App\Models\ContentType;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

it('keeps root category browse page queries bounded when rendering many child category pills', function (): void {
    $contentType = ContentType::create([
        'name' => 'Skins',
        'slug' => 'skins',
        'description' => 'Skins content type',
    ]);

    $category = Category::create([
        'content_type_id' => $contentType->id,
        'name' => 'Winstep Full Pak',
        'slug' => 'winstep-full-pak',
        'description' => 'Winstep suite skins',
        'is_active' => true,
        'sort_order' => 1,
    ]);

    for ($i = 1; $i <= 20; $i++) {
        Category::create([
            'content_type_id' => $contentType->id,
            'parent_id' => $category->id,
            'name' => 'Child Category ' . $i,
            'slug' => 'child-category-' . $i,
            'description' => 'Nested category ' . $i,
            'is_active' => true,
            'sort_order' => $i,
        ]);
    }

    $categoryQueryCount = 0;
    DB::listen(function ($query) use (&$categoryQueryCount): void {
        if (preg_match('/\b(from|join)\s+["`\[]?(categories|content_types)\b/i', $query->sql) === 1) {
            $categoryQueryCount++;
        }
    });

    $this->get('/skins/winstep-full-pak')
        ->assertOk()
        ->assertSee('Winstep Full Pak')
        ->assertSee('Child Category 1')
        ->assertSee('Child Category 20');

    expect($categoryQueryCount)->toBeLessThanOrEqual(14);
});

it('keeps category browse artwork card relation queries bounded', function (): void {
    $contentType = ContentType::create([
        'name' => 'Skins',
        'slug' => 'skins',
        'description' => 'Skins content type',
    ]);

    $category = Category::create([
        'content_type_id' => $contentType->id,
        'name' => 'Misc',
        'slug' => 'misc',
        'description' => 'Misc skins',
        'is_active' => true,
        'sort_order' => 1,
    ]);

    $artworks = collect();

    for ($i = 1; $i <= 12; $i++) {
        $user = User::factory()->create([
            'username' => 'miscbrowseuser' . $i,
        ]);

        $artwork = Artwork::factory()->for($user)->create([
            'title' => 'Misc Browse Artwork ' . $i,
            'slug' => 'misc-browse-artwork-' . $i,
            'published_at' => now()->subMinutes($i),
            'is_public' => true,
            'is_approved' => true,
        ]);

        $artwork->categories()->attach($category->id);
        $artworks->push($artwork);
    }

    Cache::put(
        'gallery.cat.catalog-visible.v4.' . md5('skins|misc') . '.trending.1',
        new LengthAwarePaginator(
            new EloquentCollection($artworks->all()),
            $artworks->count(),
            24,
            1,
            [
                'path' => url('/skins/misc'),
                'query' => [],
            ]
        ),
        300,
    );

    $relationQueryCount = 0;
    DB::listen(function ($query) use (&$relationQueryCount): void {
        if (preg_match('/\b(from|join)\s+["`\[]?(users|user_profiles|categories|content_types|groups)\b/i', $query->sql) === 1) {
            $relationQueryCount++;
        }
    });

    $this->get('/skins/misc')
        ->assertOk()
        ->assertSee('Misc')
        ->assertSee('Misc Browse Artwork 1')
        ->assertSee('Misc Browse Artwork 12');

    expect($relationQueryCount)->toBeLessThanOrEqual(12);
});