<?php

namespace Tests\Feature;

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

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'sub_show_orders_batch_run_id_plan',
            'name' => '订阅详情订单表批次run_id列测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $sub = SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_SHOW_ORDERS_BATCH_RUN_ID_0001',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 10,
            'starts_at' => now()->subDay(),
            'ends_at' => now()->addMonth(),
            'activated_at' => now()->subDay(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'site_subscription_id' => $sub->id,
            'order_no' => 'PO_SUB_SHOW_ORDERS_BATCH_RUN_ID_0001',
            'order_type' => 'renewal',
            'status' => 'activated',
            'payment_status' => 'paid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 10,
            'placed_at' => now(),
            'paid_at' => now(),
            'activated_at' => now(),
            'meta' => [
                'batch_activation' => ['run_id' => 'RUN_BAS_SUB_0001'],
                'batch_mark_paid_and_activate' => ['run_id' => 'RUN_BMPA_SUB_0001'],
            ],
        ]);

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

        // 表头列必须存在
        $res->assertSee('BAS run_id', false);
        $res->assertSee('BMPA run_id', false);

        // run_id 值应可点击直达批次复盘页（带 back 回到订阅详情）
        $expectedBack = urlencode('/admin/site-subscriptions/' . $sub->id);

        $res->assertSee('/admin/platform-batches/show?type=bas&run_id=RUN_BAS_SUB_0001&back=' . $expectedBack, false);
        $res->assertSee('/admin/platform-batches/show?type=bmpa&run_id=RUN_BMPA_SUB_0001&back=' . $expectedBack, false);

        // 仍应保留订单详情 link
        $res->assertSee('/admin/platform-orders/' . $order->id . '?back=' . $expectedBack, false);
    }
}
