<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Domain\Plugin\PluginManager;
use Tests\TestCase;

class PluginDocsTest extends TestCase
{
    public function test_paid_content_docs_render_chinese_html_under_zh_cn(): void
    {
        app()->setLocale('zh_CN');
        $manager = app(PluginManager::class);

        $markdown = $manager->readDocs('larablog/paid-content');
        $html = $manager->readDocsHtml('larablog/paid-content');

        $this->assertNotNull($markdown);
        $this->assertNotNull($html);
        $this->assertStringContainsString('付费内容', $markdown);
        $this->assertStringContainsString('<h1', $html);
        $this->assertStringContainsString('付费内容', $html);
        $this->assertStringContainsString('<strong>', $html);
        $this->assertStringNotContainsString('# Paid Content', (string) $markdown);
        $this->assertStringNotContainsString('# 付费内容', $html);
    }

    public function test_docs_prefer_locale_file_then_fallback_to_readme(): void
    {
        $root = storage_path('framework/testing/plugins-'.uniqid());
        $pluginDir = $root.'/acme/docsdemo';
        mkdir($pluginDir, 0777, true);
        file_put_contents($pluginDir.'/plugin.json', json_encode([
            'name' => 'acme/docsdemo',
            'title' => 'Docs demo',
            'version' => '1.0.0',
            'docs' => 'README.md',
        ]));
        file_put_contents($pluginDir.'/README.md', "# English\n\nHello **world**.");
        file_put_contents($pluginDir.'/README.zh_CN.md', "# 中文说明\n\n这是**加粗**。\n");

        config(['larablog.plugin_path' => $root]);
        $manager = app(PluginManager::class);

        try {
            $zh = (string) $manager->readDocs('acme/docsdemo', 'zh_CN');
            $this->assertStringContainsString('中文说明', $zh);

            $html = (string) $manager->readDocsHtml('acme/docsdemo', 'zh_CN');
            $this->assertStringContainsString('<h1', $html);
            $this->assertStringContainsString('中文说明', $html);
            $this->assertStringContainsString('<strong>', $html);
            $this->assertStringNotContainsString('# 中文说明', $html);

            $en = (string) $manager->readDocs('acme/docsdemo', 'en');
            $this->assertStringContainsString('English', $en);
            $this->assertStringNotContainsString('中文说明', $en);
        } finally {
            unlink($pluginDir.'/README.zh_CN.md');
            unlink($pluginDir.'/README.md');
            unlink($pluginDir.'/plugin.json');
            rmdir($pluginDir);
            rmdir($root.'/acme');
            rmdir($root);
        }
    }

    public function test_unsafe_locale_does_not_escape_plugin_directory(): void
    {
        $root = storage_path('framework/testing/plugins-'.uniqid());
        $pluginDir = $root.'/acme/safe';
        mkdir($pluginDir, 0777, true);
        file_put_contents($pluginDir.'/plugin.json', json_encode([
            'name' => 'acme/safe',
            'title' => 'Safe',
            'version' => '1.0.0',
            'docs' => 'README.md',
        ]));
        file_put_contents($pluginDir.'/README.md', "# Safe\n");

        config(['larablog.plugin_path' => $root]);
        $manager = app(PluginManager::class);

        try {
            $path = $manager->docsPath('acme/safe', '../..');
            $this->assertNotNull($path);
            $this->assertSame(realpath($pluginDir.'/README.md'), $path);
            $this->assertStringContainsString('Safe', (string) $manager->readDocs('acme/safe', '../..'));
        } finally {
            unlink($pluginDir.'/README.md');
            unlink($pluginDir.'/plugin.json');
            rmdir($pluginDir);
            rmdir($root.'/acme');
            rmdir($root);
        }
    }
}
