<?php

use App\Http\Controllers\Web\BrowseGalleryController;

it('sorts latest content-type gallery pages by published date instead of draft creation date', function (): void {
    $sortMap = (new ReflectionClass(BrowseGalleryController::class))->getConstant('SORT_MAP');

    expect($sortMap['latest'] ?? null)->toBe(['published_at_ts:desc']);
    expect($sortMap['oldest'] ?? null)->toBe(['published_at_ts:asc']);
});

it('uses published date as the recency tie-breaker on default content-type explore pages', function (): void {
    $sortMap = (new ReflectionClass(BrowseGalleryController::class))->getConstant('SORT_MAP');
    $cacheVersion = (new ReflectionClass(BrowseGalleryController::class))->getConstant('CACHE_VERSION');

    expect($sortMap['trending'] ?? null)->toBe([
        'trending_score_24h:desc',
        'trending_score_7d:desc',
        'favorites_count:desc',
        'published_at_ts:desc',
    ]);

    expect($sortMap['fresh'] ?? null)->toBe([
        'published_at_ts:desc',
        'trending_score_7d:desc',
        'favorites_count:desc',
    ]);

    expect($cacheVersion)->toBe('v4');
});

it('anchors category gallery filters to the content type and all descendant category slugs', function (): void {
    $controller = app(BrowseGalleryController::class);
    $method = new ReflectionMethod(BrowseGalleryController::class, 'categoryPageFilterExpression');
    $method->setAccessible(true);

    $filter = $method->invoke($controller, 'skins', ['audio', 'winamp', 'aplayer']);

    expect($filter)->toBe(
        'is_public = true AND is_approved = true AND '
        . '(content_type = "skins" OR content_types = "skins") '
        . 'AND ('
        . '(category = "audio" OR categories = "audio") OR '
        . '(category = "winamp" OR categories = "winamp") OR '
        . '(category = "aplayer" OR categories = "aplayer")'
        . ')'
    );
});