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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'batch_activate_receipt_status_filter_fields_test',
            'name' => '批量同步回执筛选字段测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // A：可同步 + 有回执
        $orderWithReceipt = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BATCH_ACTIVATE_RECEIPT_HAS_0001',
            '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' => [
                'payment_summary' => [
                    'count' => 1,
                    'total_amount' => 10,
                ],
            ],
        ]);

        // B：可同步 + 无回执（广义）（用于验证 receipt_status=has 不会误命中）
        $orderNoReceipt = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BATCH_ACTIVATE_RECEIPT_NONE_0002',
            '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' => [],
        ]);

        // 访问列表页：确保批量同步表单透传 receipt_status
        $page = $this->get('/admin/platform-orders?syncable_only=1&receipt_status=has');
        $page->assertOk();
        $page->assertSee('name="receipt_status"', false);
        $page->assertSee('value="has"', false);

        // 执行批量同步（filtered scope 且必须 syncable_only=1 才能提交）
        $res = $this->post('/admin/platform-orders/batch-activate-subscriptions', [
            'scope' => 'filtered',
            'syncable_only' => '1',
            'receipt_status' => 'has',
            'limit' => 50,
        ]);

        $res->assertRedirect();

        $orderWithReceipt->refresh();
        $orderNoReceipt->refresh();

        $this->assertNotEmpty(data_get($orderWithReceipt->meta, 'subscription_activation.subscription_id'));
        $this->assertEmpty(data_get($orderNoReceipt->meta, 'subscription_activation.subscription_id'));
    }
}
