<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Blog\Services\PostContentRenderer;
use App\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Plugins\Neatstudio\LinkTracker\Models\Redirect;
use Tests\TestCase;

class GoRedirectTest extends TestCase
{
    use RefreshDatabase;

    public function test_tracked_url_rewrites_external_but_keeps_internal(): void
    {
        // 外链中转插件默认启用，link.redirect 过滤器生效
        $this->assertStringContainsString('/go/', tracked_url('https://example.com/path'));
        $this->assertSame('http://laralog.test/posts/1', tracked_url('http://laralog.test/posts/1'));
        $this->assertSame('/posts/1', tracked_url('/posts/1'));
        $this->assertSame('mailto:a@b.com', tracked_url('mailto:a@b.com'));
        $this->assertSame('#anchor', tracked_url('#anchor'));
    }

    public function test_tracked_url_returns_original_without_plugin_filter(): void
    {
        // 无插件注册 link.redirect 过滤器时（插件停用/未装），核心视图原样返回外链
        $manager = new \App\Blog\Support\PluginManager();

        $this->assertSame('https://example.com/path', $manager->applyFilters('link.redirect', 'https://example.com/path'));
    }

    public function test_go_redirect_counts_clicks(): void
    {
        $redirect = Redirect::create([
            'hash' => 'abc123def0',
            'target_url' => 'https://example.com/target',
        ]);

        $this->get('/go/abc123def0')
            ->assertRedirect('https://example.com/target');

        $redirect->refresh();
        $this->assertSame(1, $redirect->clicks);
        $this->assertNotNull($redirect->last_clicked_at);
    }

    public function test_disabled_redirect_returns_410(): void
    {
        Redirect::create([
            'hash' => 'blocked0000',
            'target_url' => 'https://example.com/bad',
            'status' => 'disabled',
        ]);

        $this->get('/go/blocked0000')->assertStatus(410);
    }

    public function test_same_url_reuses_same_hash(): void
    {
        $a = tracked_url('https://example.com/dup');
        $b = tracked_url('https://example.com/dup');

        $this->assertSame($a, $b);
        $this->assertSame(1, Redirect::query()->where('target_url', 'https://example.com/dup')->count());
    }

    public function test_post_render_rewrites_external_links(): void
    {
        $post = Post::create([
            'title' => '外链测试',
            'slug' => 'go-test',
            'content' => "正文 <a href=\"https://example.com/x\" target=\"_blank\">外链</a> 和 <a href=\"http://laralog.test/posts/1\">站内</a>",
            'content_format' => 'html',
            'status' => 'published',
            'published_at' => now(),
        ]);

        $html = app(PostContentRenderer::class)->render($post);

        $this->assertStringContainsString('href="http://laralog.test/go/', $html);
        $this->assertStringContainsString('href="http://laralog.test/posts/1"', $html);
        $this->assertStringNotContainsString('href="https://example.com/x"', $html);
    }

    public function test_comment_body_autolinks_and_redirects(): void
    {
        $html = comment_body('看看 https://example.com/note 这个链接');

        $this->assertStringContainsString('href="http://laralog.test/go/', $html);
        $this->assertStringContainsString('rel="nofollow noopener"', $html);
        $this->assertStringNotContainsString('href="https://example.com/note"', $html);
    }
}
