<?php

namespace Tests\Feature;

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

class AdminPlanFormBackLinkNotEscapedTest 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_create_form_back_link_should_not_escape_ampersand(): void
    {
        $this->loginAsPlatformAdmin();

        $back = '/admin/plans?status=active&keyword=test';

        $res = $this->get('/admin/plans/create?back=' . urlencode($back));
        $res->assertOk();

        $res->assertSee('href="' . $back . '"', false);
        $res->assertDontSee('href="' . str_replace('&', '&amp;', $back) . '"', false);
    }

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

        $plan = Plan::query()->create([
            'code' => 'plan_form_back_01',
            'name' => 'plan form back',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $back = '/admin/plans?status=inactive&keyword=test';

        $res = $this->get('/admin/plans/' . $plan->id . '/edit?back=' . urlencode($back));
        $res->assertOk();

        $res->assertSee('href="' . $back . '"', false);
        $res->assertDontSee('href="' . str_replace('&', '&amp;', $back) . '"', false);
    }
}
