<?php

namespace Tests\Feature;

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

class ThemeTest extends TestCase
{
    use RefreshDatabase;

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

        $this->seed();

        $user = User::create(['name' => '作者', 'email' => 't@test.com', 'password' => bcrypt('x')]);
        $category = Category::create(['name' => '分类', 'slug' => 'cat']);
        Post::create([
            'user_id' => $user->id,
            'category_id' => $category->id,
            'title' => '主题测试',
            'slug' => 'theme-test',
            'content' => '内容',
            'content_format' => 'markdown',
            'status' => 'published',
            'published_at' => now(),
        ]);
    }

    public function test_sablog_theme_renders_classic_layout(): void
    {
        app(\App\Blog\Support\ThemeManager::class)->activate('sablog');
        $this->get('/')->assertOk()->assertSee('outmain');
        $this->get('/posts/theme-test.shtml')->assertOk()->assertSee('outmain');
    }

    public function test_modern_theme_renders_modern_layout(): void
    {
        app(\App\Blog\Support\ThemeManager::class)->activate('modern');
        $this->get('/')->assertOk()->assertSee('m-header');
        $this->get('/posts/theme-test.shtml')->assertOk()->assertSee('m-header');
    }

    public function test_theme_assets_are_served(): void
    {
        $this->get('/themes/sablog/assets/style.css')->assertOk()->assertHeader('Content-Type', 'text/css; charset=utf-8');
        $this->get('/themes/modern/assets/style.css')->assertOk();
        $this->get('/themes/nonexistent/assets/style.css')->assertNotFound();
    }

    public function test_unknown_theme_falls_back_to_default(): void
    {
        Setting::set('active_theme', 'does-not-exist');
        $this->get('/')->assertOk();
    }
}
