<?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\ForumTopic;

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

it('renders discussion forum structured data on forum topic pages', function (): void {
    $author = User::query()->create([
        'username' => 'forumauthor',
        'username_changed_at' => now()->subDays(120),
        'last_username_change_at' => now()->subDays(120),
        'onboarding_step' => 'complete',
        'name' => 'Forum Author',
        'email' => 'forumauthor@example.com',
        'email_verified_at' => now(),
        'password' => 'password',
        'is_active' => true,
    ]);

    $replier = User::query()->create([
        'username' => 'forumreplier',
        'username_changed_at' => now()->subDays(120),
        'last_username_change_at' => now()->subDays(120),
        'onboarding_step' => 'complete',
        'name' => 'Forum Replier',
        'email' => 'forumreplier@example.com',
        'email_verified_at' => now(),
        'password' => 'password',
        'is_active' => true,
    ]);

    $category = ForumCategory::query()->create([
        'name' => 'Forum SEO',
        'title' => 'Forum SEO',
        'slug' => 'forum-seo',
        'description' => 'SEO discussion category',
        'is_active' => true,
        'position' => 1,
    ]);

    $board = ForumBoard::query()->create([
        'category_id' => $category->id,
        'title' => 'Technical SEO',
        'slug' => 'technical-seo',
        'description' => 'Technical SEO board',
        'is_active' => true,
        'position' => 1,
    ]);

    $topic = ForumTopic::query()->create([
        'board_id' => $board->id,
        'user_id' => $author->id,
        'title' => 'Structured data for forums',
        'slug' => 'structured-data-for-forums',
        'views' => 42,
        'replies_count' => 1,
        'last_post_at' => now(),
    ]);

    ForumPost::query()->create([
        'thread_id' => $topic->id,
        'topic_id' => $topic->id,
        'user_id' => $author->id,
        'content' => 'Original post body about Google discussion structured data.',
        'created_at' => now()->subHour(),
        'updated_at' => now()->subHour(),
    ]);

    $reply = ForumPost::query()->create([
        'thread_id' => $topic->id,
        'topic_id' => $topic->id,
        'user_id' => $replier->id,
        'content' => 'Reply with implementation details for the forum page.',
        'created_at' => now()->subMinutes(15),
        'updated_at' => now()->subMinutes(15),
    ]);

    $response = $this->get(route('forum.topic.show', ['topic' => $topic->slug]));

    $response
        ->assertOk()
        ->assertSee('application/ld+json', false)
        ->assertSee('DiscussionForumPosting', false)
        ->assertSee('<script type="application/ld+json">{"@context":"https://schema.org","@type":"DiscussionForumPosting"', false)
        ->assertSee('"comment":[{"@type":"Comment"', false)
        ->assertSee('itemtype="https://schema.org/DiscussionForumPosting"', false)
        ->assertSee('itemprop="author"', false)
        ->assertSee('itemprop="text"', false)
        ->assertSee('itemprop="comment"', false)
        ->assertSee('itemtype="https://schema.org/Comment"', false)
        ->assertSee('itemprop="headline"', false)
        ->assertSee('itemprop="mainEntityOfPage"', false)
        ->assertSee('Structured data for forums', false)
        ->assertSee('Original post body about Google discussion structured data.', false)
        ->assertSee('Reply with implementation details for the forum page.', false)
        ->assertSee(route('forum.topic.show', ['topic' => $topic->slug]) . '#post-' . $reply->id, false)
        ->assertSee(route('profile.show', ['username' => 'forumauthor']), false)
        ->assertSee(route('profile.show', ['username' => 'forumreplier']), false);
});

it('falls back to the topic title when the opening post has no body', function (): void {
    $author = User::query()->create([
        'username' => 'forumfallbackauthor',
        'username_changed_at' => now()->subDays(120),
        'last_username_change_at' => now()->subDays(120),
        'onboarding_step' => 'complete',
        'name' => 'Forum Fallback Author',
        'email' => 'forumfallbackauthor@example.com',
        'email_verified_at' => now(),
        'password' => 'password',
        'is_active' => true,
    ]);

    $category = ForumCategory::query()->create([
        'name' => 'Forum SEO',
        'title' => 'Forum SEO',
        'slug' => 'forum-seo-fallback',
        'description' => 'SEO discussion category',
        'is_active' => true,
        'position' => 1,
    ]);

    $board = ForumBoard::query()->create([
        'category_id' => $category->id,
        'title' => 'Technical SEO',
        'slug' => 'technical-seo-fallback',
        'description' => 'Technical SEO board',
        'is_active' => true,
        'position' => 1,
    ]);

    $topic = ForumTopic::query()->create([
        'board_id' => $board->id,
        'user_id' => $author->id,
        'title' => 'Sparse topic body',
        'slug' => 'sparse-topic-body',
        'views' => 7,
        'replies_count' => 0,
        'last_post_at' => now(),
    ]);

    ForumPost::query()->create([
        'thread_id' => $topic->id,
        'topic_id' => $topic->id,
        'user_id' => $author->id,
        'content' => '',
        'created_at' => now()->subHour(),
        'updated_at' => now()->subHour(),
    ]);

    $this->get(route('forum.topic.show', ['topic' => $topic->slug]))
        ->assertOk()
        ->assertSee('itemtype="https://schema.org/DiscussionForumPosting"', false)
        ->assertSee('itemprop="author"', false)
        ->assertSee('itemprop="text"', false)
        ->assertSee('Sparse topic body', false)
        ->assertSee('No comments yet.', false)
        ->assertSee(route('profile.show', ['username' => 'forumfallbackauthor']), false);
});

it('falls back to a default author when the topic author is missing', function (): void {
    $deletedAuthor = User::query()->create([
        'username' => 'forumdeletedauthor',
        'username_changed_at' => now()->subDays(120),
        'last_username_change_at' => now()->subDays(120),
        'onboarding_step' => 'complete',
        'name' => 'Deleted Forum Author',
        'email' => 'forumdeletedauthor@example.com',
        'email_verified_at' => now(),
        'password' => 'password',
        'is_active' => true,
    ]);

    $category = ForumCategory::query()->create([
        'name' => 'Forum Missing Author',
        'title' => 'Forum Missing Author',
        'slug' => 'forum-missing-author',
        'description' => 'Category for orphaned forum topics',
        'is_active' => true,
        'position' => 1,
    ]);

    $board = ForumBoard::query()->create([
        'category_id' => $category->id,
        'title' => 'Missing Author Board',
        'slug' => 'missing-author-board',
        'description' => 'Board for orphaned forum topics',
        'is_active' => true,
        'position' => 1,
    ]);

    $topic = ForumTopic::query()->create([
        'board_id' => $board->id,
        'user_id' => $deletedAuthor->id,
        'title' => 'Missing author topic',
        'slug' => 'missing-author-topic',
        'views' => 3,
        'replies_count' => 0,
        'last_post_at' => now(),
    ]);

    ForumPost::query()->create([
        'thread_id' => $topic->id,
        'topic_id' => $topic->id,
        'user_id' => $deletedAuthor->id,
        'content' => '',
        'created_at' => now()->subHour(),
        'updated_at' => now()->subHour(),
    ]);

    $deletedAuthor->delete();

    $this->get(route('forum.topic.show', ['topic' => $topic->slug]))
        ->assertOk()
        ->assertSee('DiscussionForumPosting', false)
        ->assertSee('"author":{"@type":"Person","name":"Skinbase"}', false)
        ->assertSee('"text":"Missing author topic"', false)
        ->assertSee('"comment":[{"@type":"Comment"', false)
        ->assertSee('No comments yet.', false);
});

it('renders item list microdata on forum board pages', function (): void {
    $author = User::query()->create([
        'username' => 'boardauthor',
        'username_changed_at' => now()->subDays(120),
        'last_username_change_at' => now()->subDays(120),
        'onboarding_step' => 'complete',
        'name' => 'Board Author',
        'email' => 'boardauthor@example.com',
        'email_verified_at' => now(),
        'password' => 'password',
        'is_active' => true,
    ]);

    $category = ForumCategory::query()->create([
        'name' => 'Forum Boards',
        'title' => 'Forum Boards',
        'slug' => 'forum-boards',
        'description' => 'Board category',
        'is_active' => true,
        'position' => 1,
    ]);

    $board = ForumBoard::query()->create([
        'category_id' => $category->id,
        'title' => 'Board Microdata',
        'slug' => 'board-microdata',
        'description' => 'Board microdata description',
        'is_active' => true,
        'position' => 1,
    ]);

    $topic = ForumTopic::query()->create([
        'board_id' => $board->id,
        'user_id' => $author->id,
        'title' => 'Board topic title',
        'slug' => 'board-topic-title',
        'replies_count' => 2,
        'created_at' => now()->subHour(),
        'last_post_at' => now(),
    ]);

    ForumPost::query()->create([
        'thread_id' => $topic->id,
        'topic_id' => $topic->id,
        'user_id' => $author->id,
        'content' => 'Board topic opening post content.',
        'created_at' => now()->subHour(),
        'updated_at' => now()->subHour(),
    ]);

    $this->get(route('forum.board.show', ['boardSlug' => $board->slug]))
        ->assertOk()
        ->assertSee('itemtype="https://schema.org/CollectionPage"', false)
        ->assertSee('itemtype="https://schema.org/ItemList"', false)
        ->assertSee('itemtype="https://schema.org/ListItem"', false)
        ->assertSee('itemtype="https://schema.org/DiscussionForumPosting"', false)
        ->assertSee('itemprop="datePublished"', false)
        ->assertSee('Board topic title', false)
        ->assertSee(route('forum.topic.show', ['topic' => $topic->slug]), false)
        ->assertSee(route('profile.show', ['username' => 'boardauthor']), false);
});

it('renders item list microdata on forum section pages', function (): void {
    $category = ForumCategory::query()->create([
        'name' => 'Photography',
        'title' => 'Photography',
        'slug' => 'photography-section-microdata',
        'description' => 'Photography category',
        'is_active' => true,
        'position' => 1,
    ]);

    $board = ForumBoard::query()->create([
        'category_id' => $category->id,
        'title' => 'Photography Board',
        'slug' => 'photography-board-microdata',
        'description' => 'Photography board description',
        'is_active' => true,
        'position' => 1,
    ]);

    $this->get(route('forum.category.show', ['categorySlug' => $category->slug]))
        ->assertOk()
        ->assertSee('itemtype="https://schema.org/CollectionPage"', false)
        ->assertSee('itemtype="https://schema.org/ItemList"', false)
        ->assertSee('itemtype="https://schema.org/ListItem"', false)
        ->assertSee(route('forum.category.show', ['categorySlug' => $category->slug]), false)
        ->assertSee(route('forum.board.show', ['boardSlug' => $board->slug]), false)
        ->assertSee('Photography boards', false)
        ->assertSee('Photography Board', false);
});