<?php

namespace Tests\Feature;

use App\Models\Merchant;
use App\Models\SiteSubscription;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

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

        $merchant = Merchant::query()->firstOrFail();

        $sub = SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_SHOW_FILTER_BACK_0001',
            'plan_name' => '测试套餐',
            'billing_cycle' => 'monthly',
            'period_months' => 1,
            'amount' => 10,
            'starts_at' => now()->subDay(),
            'ends_at' => now()->addDays(29),
            'activated_at' => now()->subDay(),
        ]);

        $back = '/admin/platform-orders?status=pending&keyword=abc';
        $expectedBack = urlencode($back);

        $res = $this->get('/admin/site-subscriptions/' . $sub->id . '?back=' . $expectedBack);
        $res->assertOk();

        // “全部”应保留 back
        $res->assertSee('href="/admin/site-subscriptions/' . $sub->id . '?back=' . $expectedBack . '"', false);

        // “可同步”应同时带 back + order_sync_status，且 & 不应被 escape 成 &amp;
        $res->assertSee('href="/admin/site-subscriptions/' . $sub->id . '?back=' . $expectedBack . '&order_sync_status=syncable"', false);
        $res->assertDontSee('back=' . $expectedBack . '&amp;order_sync_status=syncable', false);
    }
}
