<?php

namespace Tests\Feature;

use App\Models\Category;
use App\Models\Comment;
use App\Models\Post;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Tags\Tag;
use Tests\TestCase;

class LegacyRoutesTest extends TestCase
{
    use RefreshDatabase;

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

        $this->seed();

        $user = User::create(['name' => '作者', 'email' => 'author@test.com', 'password' => bcrypt('x')]);
        $category = Category::create(['name' => '技术', 'slug' => 'tech']);
        \Illuminate\Support\Facades\DB::table('posts')->insert([
            'id' => 12,
            'user_id' => $user->id,
            'category_id' => $category->id,
            'title' => '老文章',
            'slug' => 'legacy-post',
            'content' => '内容',
            'content_format' => 'markdown',
            'status' => 'published',
            'published_at' => now(),
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        Comment::create(['post_id' => 12, 'author_name' => '访客', 'content' => '好文', 'status' => 'published', 'created_at' => now()]);
    }

    public function test_rewrite_style_urls_redirect(): void
    {
        $this->get('/show-12.html')->assertRedirect('/posts/legacy-post.shtml');
        $this->get('/show-12-1.html')->assertRedirect('/posts/legacy-post.shtml');
        $this->get('/category-1.html')->assertRedirect();
        $this->get('/archives-'.now()->format('Ymd').'.html')->assertRedirect();
        $this->get('/tagslist.html')->assertRedirect('/tags.shtml');
        $this->get('/reg.shtml')->assertRedirect('/register.shtml');
    }

    public function test_shtml_canonical_pages_render(): void
    {
        $this->get('/links.shtml')->assertOk();
        $this->get('/login.shtml')->assertOk();
        $this->get('/comments.shtml')->assertOk();
        $this->get('/search.shtml')->assertOk();
        $this->get('/tags.shtml')->assertOk();
        $this->get('/archives.shtml')->assertOk();
        $this->get('/posts/legacy-post.shtml')->assertOk();
    }

    public function test_no_suffix_urls_redirect_to_shtml(): void
    {
        $this->get('/links')->assertRedirect('/links.shtml');
        $this->get('/login')->assertRedirect('/login.shtml');
        $this->get('/comments')->assertRedirect('/comments.shtml');
        $this->get('/archives')->assertRedirect('/archives.shtml');
        $this->get('/posts/legacy-post')->assertRedirect('/posts/legacy-post.shtml');
    }

    public function test_query_style_urls_redirect(): void
    {
        $this->get('/?action=show&id=12')->assertRedirect('/posts/legacy-post.shtml');
        $this->get('/?action=category&cid=1')->assertRedirect();
        $this->get('/?action=index&setdate='.now()->format('Ym'))->assertRedirect();
        $this->get('/?action=search&keyword=laravel')->assertRedirect('/search.shtml?q=laravel');
        $this->get('/?action=links')->assertRedirect('/links.shtml');
        $this->get('/?action=archives')->assertRedirect('/archives.shtml');
        $this->get('/?action=comments')->assertRedirect('/comments.shtml');
    }

    public function test_trackback_endpoints_are_gone(): void
    {
        $this->get('/tburl.php')->assertStatus(410);
        $this->get('/trackback.php')->assertStatus(410);
    }

    public function test_stable_paths_work(): void
    {
        $this->get('/rss.xml')->assertOk();
        $this->get('/sitemap.xml')->assertOk();
        $this->get('/robots.txt')->assertOk();
    }

    public function test_missing_records_return_404(): void
    {
        $this->get('/show-99999.html')->assertNotFound();
        $this->get('/?action=show&id=99999')->assertNotFound();
    }

    public function test_legacy_tag_id_mapping(): void
    {
        $tag = Tag::create(['name' => 'laravel-tag', 'type' => 'post']);
        Setting::set('legacy_tags', [1 => $tag->id]);

        $this->get('/?action=tags&id=1')->assertRedirect('/tags/laravel-tag.shtml');
    }
}
