<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AdminFlashWarningShouldSupportOptionalLinkTest extends TestCase
{
    use RefreshDatabase;

    protected function loginAsPlatformAdmin(): void
    {
        $this->seed();

        $this->post('/admin/login', [
            'email' => 'platform.admin@demo.local',
            'password' => 'Platform@123456',
        ])->assertRedirect('/admin');
    }

    public function test_flash_warning_should_render_optional_link_when_session_keys_present(): void
    {
        $this->loginAsPlatformAdmin();

        $res = $this->withSession([
            'warning' => '检测到重复提交',
            'warning_link_href' => '/admin/platform-batches/show?type=bmpa&run_id=BMPA202603171234560001',
            'warning_link_label' => '进入上次批次复盘',
        ])->get('/admin');

        $res->assertOk();
        $res->assertSee('检测到重复提交');
        $res->assertSee('进入上次批次复盘');
        $res->assertSee('/admin/platform-batches/show?type=bmpa&amp;run_id=BMPA202603171234560001', false);
    }

    public function test_flash_warning_link_should_be_sanitized_to_relative_path(): void
    {
        $this->loginAsPlatformAdmin();

        $res = $this->withSession([
            'warning' => 'warn',
            'warning_link_href' => 'https://evil.example.com/x',
            'warning_link_label' => '查看',
        ])->get('/admin');

        $res->assertOk();
        $res->assertDontSee('https://evil.example.com/x');
    }
}
