<?php

namespace Tests\Feature\Marketing;

use App\Models\BlogCategory;
use App\Models\BlogPost;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;

class BlogShowTest extends TestCase
{
    use RefreshDatabase;

    public function test_blog_show_returns_headings_and_adjacent_posts(): void
    {
        $category = BlogCategory::query()->create([
            'slug' => 'blog',
            'name' => [
                'de' => 'Blog',
                'en' => 'Blog',
            ],
            'description' => [
                'de' => 'Hochzeitsblog',
                'en' => 'Wedding blog',
            ],
            'is_visible' => true,
        ]);

        $older = BlogPost::query()->create([
            'blog_category_id' => $category->id,
            'slug' => 'older-story',
            'title' => [
                'de' => 'Ältere Story',
                'en' => 'Older Story',
            ],
            'excerpt' => [
                'de' => '**Alt**',
                'en' => '**Old**',
            ],
            'content' => [
                'de' => '## Start\nÄlterer Inhalt',
                'en' => '## Start\nOlder content',
            ],
            'is_published' => true,
            'published_at' => Carbon::parse('2024-01-01'),
        ]);

        $current = BlogPost::query()->create([
            'blog_category_id' => $category->id,
            'slug' => 'spotlight-story',
            'title' => [
                'de' => 'Spotlight Story',
                'en' => 'Spotlight Story',
            ],
            'excerpt' => [
                'de' => 'Zusammenfassung',
                'en' => 'Summary',
            ],
            'content' => [
                'de' => "## Einleitung\nWillkommen\n### Schritte\nLos geht's",
                'en' => "## Intro\nWelcome\n### Steps\nLet's go",
            ],
            'is_published' => true,
            'published_at' => Carbon::parse('2024-02-01'),
        ]);

        $newer = BlogPost::query()->create([
            'blog_category_id' => $category->id,
            'slug' => 'new-story',
            'title' => [
                'de' => 'Neue Story',
                'en' => 'New Story',
            ],
            'excerpt' => [
                'de' => 'Neu',
                'en' => 'New',
            ],
            'content' => [
                'de' => '## Vorschau\nNeuer Inhalt',
                'en' => '## Preview\nNew content',
            ],
            'is_published' => true,
            'published_at' => Carbon::parse('2024-03-01'),
        ]);

        $response = $this->get('/de/blog/'.$current->slug);

        $response->assertOk();

        $response->assertInertia(fn (Assert $page) => $page
            ->component('marketing/BlogShow')
            ->where('post.previous_post.slug', $older->slug)
            ->where('post.next_post.slug', $newer->slug)
            ->where('post.headings.0.text', 'Einleitung')
            ->where('post.headings.0.slug', fn ($slug) => is_string($slug) && str_contains($slug, 'einleitung'))
            ->where('post.content_html', fn ($html) => is_string($html) && str_contains($html, 'id="einleitung"'))
        );
    }
}
