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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'batch_activate_subscriptions_refund_status_plan',
            'name' => '批量同步订阅（退款筛选口径）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // A：无退款，可同步
        $a = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BAS_REFUND_A',
            'order_type' => 'new_purchase',
            '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' => [],
        ]);

        // B：有退款（refund_summary），仍满足“已支付+已生效+未同步”，但应被 refund_status=none 排除
        $b = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BAS_REFUND_B',
            'order_type' => 'new_purchase',
            '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' => [
                'refund_summary' => [
                    'count' => 1,
                    'total_amount' => 1.00,
                ],
            ],
        ]);

        // 批量同步订阅：filtered scope 必须带 syncable_only=1；并附加 refund_status=none
        $this->post('/admin/platform-orders/batch-activate-subscriptions', [
            'scope' => 'filtered',
            'syncable_only' => '1',
            'refund_status' => 'none',
            'limit' => 50,
        ])->assertRedirect();

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

        // A 应被同步（meta.subscription_activation.subscription_id 存在）
        $this->assertNotNull(data_get($a->meta, 'subscription_activation.subscription_id'));

        // B 不应被同步
        $this->assertNull(data_get($b->meta, 'subscription_activation.subscription_id'));
    }
}
