<?php

use App\Http\Middleware\HandleInertiaRequests;
use App\Models\User;
use cPad\Plugins\Forum\Models\ForumBoard;
use cPad\Plugins\Forum\Models\ForumCategory;
use cPad\Plugins\Forum\Models\ForumPost;
use cPad\Plugins\Forum\Models\ForumPostReaction;
use cPad\Plugins\Forum\Models\ForumTopic;
use Illuminate\Support\Facades\DB;

beforeEach(function (): void {
    $this->withoutMiddleware(HandleInertiaRequests::class);
});

it('keeps forum homepage source queries bounded across many boards and trending topics', function (): void {
    $author = User::query()->create([
        'username' => 'forumhomepageauthor',
        'username_changed_at' => now()->subDays(120),
        'last_username_change_at' => now()->subDays(120),
        'onboarding_step' => 'complete',
        'name' => 'Forum Homepage Author',
        'email' => 'forumhomepageauthor@example.com',
        'email_verified_at' => now(),
        'password' => 'password',
        'is_active' => true,
    ]);

    $category = ForumCategory::query()->create([
        'name' => 'Forum Home Performance',
        'title' => 'Forum Home Performance',
        'slug' => 'forum-home-performance',
        'description' => 'Forum home performance category',
        'is_active' => true,
        'position' => 1,
    ]);

    $boards = collect();

    for ($boardIndex = 1; $boardIndex <= 6; $boardIndex++) {
        $board = ForumBoard::query()->create([
            'category_id' => $category->id,
            'title' => 'Board ' . $boardIndex,
            'slug' => 'forum-home-board-' . $boardIndex,
            'description' => 'Forum home board ' . $boardIndex,
            'is_active' => true,
            'position' => $boardIndex,
        ]);

        $boards->push($board);

        for ($topicIndex = 1; $topicIndex <= 3; $topicIndex++) {
            $topic = ForumTopic::query()->create([
                'board_id' => $board->id,
                'user_id' => $author->id,
                'title' => "Board {$boardIndex} Topic {$topicIndex}",
                'slug' => "forum-home-board-{$boardIndex}-topic-{$topicIndex}",
                'replies_count' => $topicIndex,
                'views' => 25 + $topicIndex,
                'last_post_at' => now()->subMinutes(($boardIndex * 10) + $topicIndex),
            ]);

            $post = ForumPost::query()->create([
                'thread_id' => $topic->id,
                'topic_id' => $topic->id,
                'user_id' => $author->id,
                'content' => 'Opening post for board ' . $boardIndex . ' topic ' . $topicIndex,
                'created_at' => now()->subMinutes(($boardIndex * 10) + $topicIndex + 5),
                'updated_at' => now()->subMinutes(($boardIndex * 10) + $topicIndex + 5),
            ]);

            ForumPostReaction::query()->create([
                'post_id' => $post->id,
                'user_id' => $author->id,
                'reaction' => 'thumbs_up',
            ]);
        }
    }

    $forumTopicsQueryCount = 0;
    $forumPostsQueryCount = 0;
    $forumReactionsQueryCount = 0;

    DB::listen(function ($query) use (&$forumTopicsQueryCount, &$forumPostsQueryCount, &$forumReactionsQueryCount): void {
        if (preg_match('/\bfrom\s+[`"\[]?forum_topics\b/i', $query->sql) === 1) {
            $forumTopicsQueryCount++;
        }

        if (preg_match('/\bfrom\s+[`"\[]?forum_posts\b/i', $query->sql) === 1) {
            $forumPostsQueryCount++;
        }

        if (preg_match('/\bfrom\s+[`"\[]?forum_post_reactions\b/i', $query->sql) === 1) {
            $forumReactionsQueryCount++;
        }
    });

    $this->get(route('forum.index'))
        ->assertOk()
        ->assertSee('Forum Home Performance')
        ->assertSee('Board 1')
        ->assertSee('Board 6');

    expect($forumTopicsQueryCount)->toBeLessThanOrEqual(4);
    expect($forumPostsQueryCount)->toBeLessThanOrEqual(1);
    expect($forumReactionsQueryCount)->toBeLessThanOrEqual(1);
});