<?php

namespace Tests\Feature;

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

class AuthFlowTest extends TestCase
{
    use RefreshDatabase;

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

        $this->seed();
    }

    public function test_login_form_posts_to_shtml(): void
    {
        $this->post('/login.shtml', [
            'email' => 'admin@laralog.test',
            'password' => 'password',
        ])->assertRedirect('/');

        $this->assertAuthenticated();
    }

    public function test_register_form_posts_to_shtml(): void
    {
        $this->post('/register.shtml', [
            'name' => '新用户',
            'email' => 'new@test.com',
            'password' => 'password123',
            'password_confirmation' => 'password123',
        ])->assertRedirect('/profile.shtml');

        $this->assertAuthenticated();
        $this->assertDatabaseHas('users', ['email' => 'new@test.com']);
    }

    public function test_logout_form_posts_to_shtml(): void
    {
        $user = User::where('email', 'admin@laralog.test')->first();
        $this->actingAs($user)->post('/logout.shtml')->assertRedirect('/');

        $this->assertGuest();
    }

    public function test_front_pages_use_shtml_links(): void
    {
        $user = User::create(['name' => '作者', 'email' => 'w@t.com', 'password' => bcrypt('x')]);
        \App\Models\Post::create([
            'user_id' => $user->id,
            'title' => '链接测试',
            'slug' => 'link-test',
            'content' => '内容',
            'content_format' => 'markdown',
            'status' => 'published',
            'published_at' => now(),
        ]);

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

        // 前台链接应生成 .shtml（不含硬编码无后缀路径）
        $this->assertStringContainsString('/posts/link-test.shtml', $html);
        $this->assertStringNotContainsString('href="/posts/link-test"', $html);
        $this->assertStringNotContainsString('href="/archives"', $html);
        $this->assertStringContainsString('href="http://laralog.test/archives.shtml"', $html);
    }
}
