<?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 AdminPlatformOrderBatchMarkPaidAndActivateShouldWriteRunIdTest 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_batch_mark_paid_and_activate_should_write_run_id_to_meta_on_success_and_failure(): void
    {
        $this->loginAsPlatformAdmin();

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'bmpa_write_run_id_plan',
            'name' => 'BMPA run_id 写入测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 30,
            'list_price' => 30,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // A：可处理
        $a = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BMPA_RUN_ID_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' => 30,
            'paid_amount' => 0,
            'placed_at' => now()->subMinutes(10),
        ]);

        // B：制造失败（已有回执证据但金额不一致）
        $b = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BMPA_RUN_ID_0002',
            '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' => 30,
            'paid_amount' => 0,
            'placed_at' => now()->subMinutes(9),
            'meta' => [
                'payment_summary' => [
                    'count' => 1,
                    'total_amount' => 1.00,
                ],
            ],
        ]);

        $res = $this->post('/admin/platform-orders/batch-mark-paid-and-activate', [
            'scope' => 'filtered',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'limit' => 50,
        ]);

        $res->assertRedirect();
        $res->assertSessionHas('success');

        $a->refresh();
        $b->refresh();

        $runIdA = (string) data_get($a->meta, 'batch_mark_paid_and_activate.run_id');
        $runIdB = (string) data_get($b->meta, 'batch_mark_paid_and_activate.run_id');

        $this->assertNotSame('', $runIdA);
        $this->assertNotSame('', $runIdB);
        $this->assertSame($runIdA, $runIdB);

        // 失败订单应写入 error.run_id，便于按批次追溯
        $this->assertSame($runIdB, (string) data_get($b->meta, 'batch_mark_paid_and_activate_error.run_id'));
    }
}
