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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'batch_mark_activated_renewal_missing_sub_plan',
            'name' => '批量仅标记生效跳过（续费缺订阅）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $bad = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BATCH_MARK_ACT_SKIP_RENEW_NO_SUB_0001',
            'order_type' => 'renewal',
            'status' => 'pending',
            '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()->subMinutes(10),
            'paid_at' => now()->subMinutes(5),
            'meta' => [],
        ]);

        $ok = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BATCH_MARK_ACT_SKIP_OK_0002',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            '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()->subMinutes(9),
            'paid_at' => now()->subMinutes(4),
            'meta' => [],
        ]);

        $res = $this->post('/admin/platform-orders/batch-mark-activated', [
            'scope' => 'filtered',
            'payment_status' => 'paid',
            'status' => 'pending',
            // 口径对齐：待生效语义需锁定 sync_status=unsynced（排除失败单等异常集合）
            'sync_status' => 'unsynced',
            'limit' => 50,
        ]);

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

        $bad->refresh();
        $ok->refresh();

        $this->assertSame('pending', $bad->status);
        $this->assertSame('activated', $ok->status);

        $this->assertNotEmpty(data_get($bad->meta, 'batch_mark_activated_error.message'));
        $msg = (string) $res->getSession()->get('success');
        $this->assertStringContainsString('失败', $msg);
        $this->assertStringContainsString('失败原因Top', $msg);
        $this->assertStringContainsString('续费单未绑定订阅', $msg);
    }
}
