<?php

namespace Tests\Feature;

use App\Models\User;
use App\Http\Controllers\Web\ExploreController;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Tests\TestCase;

class ExploreFiltersTest extends TestCase
{
    use RefreshDatabase;

    public function test_explore_builds_a_filter_expression_from_selected_sidebar_filters(): void
    {
        $author = User::factory()->create([
            'username' => 'catlover',
            'name' => 'Cat Lover',
        ]);

        $controller = app(ExploreController::class);
        $request = Request::create('/explore', 'GET', [
            'sort' => 'latest',
            'orientation' => 'portrait',
            'resolution' => 'fhd',
            'date_from' => '2026-04-01',
            'date_to' => '2026-04-16',
            'author' => 'catlover',
        ]);

        $reflection = new \ReflectionClass($controller);
        $method = $reflection->getMethod('buildExploreFilterExpression');
        $method->setAccessible(true);

        $filter = $method->invoke($controller, $request, null);

        $this->assertIsString($filter);
        $this->assertStringContainsString('is_public = true', $filter);
        $this->assertStringContainsString('is_approved = true', $filter);
        $this->assertStringContainsString('orientation = "portrait"', $filter);
        $this->assertStringContainsString('resolution = "1920x1080"', $filter);
        $this->assertStringContainsString('created_at >= "2026-04-01"', $filter);
        $this->assertStringContainsString('created_at <= "2026-04-16"', $filter);
        $this->assertStringContainsString('author_id = ' . $author->id, $filter);
        $this->assertStringContainsString('published_as_type = "user"', $filter);
    }
}