<?php

namespace Tests\Feature;

use App\Blog\Support\ThemeInjector;
use App\Models\Category;
use App\Models\Post;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ThemeInjectTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        $this->seed();
        \App\Blog\Support\PageCacheMiddleware::flush();

        $user = User::create(['name' => '作者', 'email' => 'i@t.com', 'password' => bcrypt('x')]);
        $cat = Category::create(['name' => '技术', 'slug' => 'tech']);
        Post::create([
            'user_id' => $user->id,
            'category_id' => $cat->id,
            'title' => '注入测试',
            'slug' => 'inject-test',
            'content' => '正文内容',
            'content_format' => 'markdown',
            'status' => 'published',
            'published_at' => now(),
        ]);
    }

    public function test_injection_blocks_render_in_head_and_body(): void
    {
        Setting::set('inject.blocks', [
            ['point' => 'head', 'title' => '统计', 'html' => '<script>window.ADMIN_TEST_STAT=1;</script>', 'sort' => 1, 'enabled' => true],
            ['point' => 'body_end', 'title' => '底部统计', 'html' => '<script>window.BOTTOM_TEST=1;</script>', 'sort' => 1, 'enabled' => true],
            ['point' => 'sidebar_top', 'title' => '广告', 'html' => '<div class="ad-block">广告位A</div>', 'sort' => 1, 'enabled' => true],
        ]);

        $html = $this->get('/')->getContent();

        $this->assertStringContainsString('ADMIN_TEST_STAT', $html);      // head 注入
        $this->assertStringContainsString('BOTTOM_TEST', $html);          // body_end 注入
        $this->assertStringContainsString('广告位A', $html);               // sidebar_top 注入
    }

    public function test_multiple_blocks_in_same_point_render_in_order(): void
    {
        Setting::set('inject.blocks', [
            ['point' => 'footer', 'title' => '第一条', 'html' => '<div id="inject-1">first</div>', 'sort' => 1, 'enabled' => true],
            ['point' => 'footer', 'title' => '第二条', 'html' => '<div id="inject-2">second</div>', 'sort' => 2, 'enabled' => true],
        ]);

        $html = $this->get('/posts/inject-test.shtml')->getContent();

        $pos1 = strpos($html, 'id="inject-1"');
        $pos2 = strpos($html, 'id="inject-2"');
        $this->assertNotFalse($pos1);
        $this->assertNotFalse($pos2);
        $this->assertLessThan($pos2, $pos1); // 按 sort 顺序
    }

    public function test_disabled_block_is_not_rendered(): void
    {
        Setting::set('inject.blocks', [
            ['point' => 'head', 'title' => '停用', 'html' => '<div id="disabled-inject">x</div>', 'sort' => 1, 'enabled' => false],
        ]);

        $this->get('/')->assertDontSee('disabled-inject');
    }

    public function test_post_content_injection_points(): void
    {
        Setting::set('inject.blocks', [
            ['point' => 'post_content_top', 'title' => '正文前广告', 'html' => '<div id="post-top-ad">TOP</div>', 'sort' => 1, 'enabled' => true],
            ['point' => 'post_content_bottom', 'title' => '正文后广告', 'html' => '<div id="post-bottom-ad">BOTTOM</div>', 'sort' => 1, 'enabled' => true],
        ]);

        $html = $this->get('/posts/inject-test.shtml')->getContent();

        $this->assertStringContainsString('post-top-ad', $html);
        $this->assertStringContainsString('post-bottom-ad', $html);
        // 顶部注入应在正文容器前
        $this->assertLessThan(strpos($html, '<div class="post-content">'), strpos($html, 'post-top-ad'));
    }

    public function test_theme_vars_are_usable(): void
    {
        Setting::set('theme_vars_sablog', ['ad_header' => '<div class="header-ad">头部广告</div>']);

        // theme('var') 直接可用
        $this->assertSame('<div class="header-ad">头部广告</div>', theme('var', 'ad_header', ''));
    }
}
