<?php

namespace Tests\Feature;

use App\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class BlogAdvancedTest extends TestCase
{
    use RefreshDatabase;

    public function test_html_post_renders_as_html_with_gallery_and_seo(): void
    {
        $post = Post::create([
            'title' => 'Advanced Post',
            'slug' => 'advanced-post',
            'excerpt' => 'A lead-in line.',
            'meta_title' => 'Custom SEO Title',
            'meta_description' => 'A custom meta description for search.',
            'body' => '<h2>Section One</h2><p>Body with <strong>bold</strong> and a <a href="/tools">link</a>.</p><ul><li>One</li></ul>',
            'gallery' => ['posts/gallery/a.jpg', 'posts/gallery/b.jpg'],
            'published_at' => now()->subDay(),
        ]);

        $res = $this->get('/blog/advanced-post');

        $res->assertOk()
            ->assertSee('article-body', false)                 // HTML render branch used
            ->assertSee('<h2>Section One</h2>', false)          // raw HTML rendered, not escaped
            ->assertSee('Custom SEO Title', false)              // meta_title used in <title>
            ->assertSee('A custom meta description for search.', false)
            ->assertSee('property="og:image"', false)           // OG tags present
            ->assertSee('storage/posts/gallery/a.jpg', false);  // gallery rendered
    }

    public function test_markdown_post_still_renders_via_legacy_parser(): void
    {
        Post::create([
            'title' => 'Markdown Post',
            'slug' => 'md-post',
            'body' => "## A Heading\n\n- a bullet\n\n> a quote",
            'published_at' => now()->subDay(),
        ]);

        $this->get('/blog/md-post')
            ->assertOk()
            ->assertSee('A Heading')
            ->assertDontSee('## A Heading')   // parsed, not literal
            ->assertSee('a bullet');
    }

    public function test_status_helper_and_visibility(): void
    {
        $draft = Post::create(['title' => 'D', 'slug' => 'd', 'body' => 'x']);
        $scheduled = Post::create(['title' => 'S', 'slug' => 's', 'body' => 'x', 'published_at' => now()->addWeek()]);
        $live = Post::create(['title' => 'L', 'slug' => 'l', 'body' => 'x', 'published_at' => now()->subWeek()]);

        $this->assertSame('draft', $draft->status());
        $this->assertSame('scheduled', $scheduled->status());
        $this->assertSame('published', $live->status());

        $this->get('/blog/d')->assertNotFound();   // draft hidden
        $this->get('/blog/s')->assertNotFound();   // scheduled hidden
        $this->get('/blog/l')->assertOk();          // live visible
    }
}
