<?php

namespace Tests\Feature;

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

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'po_show_actions_plan_01',
            'name' => '平台订单详情动作测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_SHOW_ACTIONS_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 0,
            'placed_at' => now(),
        ]);

        $res = $this->get('/admin/platform-orders/' . $order->id);

        $res->assertOk();

        // Guardrails: 收费闭环/治理能力的核心动作必须可见（避免 UI 美化时误删/隐藏）。
        $res->assertSee('标记支付并生效');
        $res->assertSee('同步订阅');

        // 操作表单（不 escape），确保 action URL 存在。
        $res->assertSee('action="/admin/platform-orders/' . $order->id . '/mark-paid-and-activate"', false);
        $res->assertSee('data-action="disable-on-submit"', false);
        $res->assertSee('action="/admin/platform-orders/' . $order->id . '/activate-subscription"', false);

        // 回执追加入口（锚点 + 表单 action）。
        $res->assertSee('id="add-payment-receipt"', false);
        $res->assertSee('action="/admin/platform-orders/' . $order->id . '/add-payment-receipt"', false);

        $res->assertSee('action="/admin/platform-orders/' . $order->id . '/add-refund-receipt"', false);
    }
}
