<?php

namespace Tests\Feature;

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

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

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

        $leadA = PlatformLead::query()->create([
            'name' => 'Lead A',
            'mobile' => '13800000001',
            'company' => 'A Inc',
            'status' => 'new',
        ]);

        $leadB = PlatformLead::query()->create([
            'name' => 'Lead B',
            'mobile' => '13800000002',
            'company' => 'B Inc',
            'status' => 'new',
        ]);

        // A：可同步 + 归属 leadA
        $a = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BAS_LEAD_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' => [
                'platform_lead_id' => $leadA->id,
            ],
        ]);

        // B：可同步 + 归属 leadB（应被 lead_id 过滤排除）
        $b = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BAS_LEAD_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' => [
                'platform_lead_id' => $leadB->id,
            ],
        ]);

        // filtered scope：同步“可同步订单”，并附加 lead_id=leadA
        $this->post('/admin/platform-orders/batch-activate-subscriptions', [
            'scope' => 'filtered',
            'syncable_only' => '1',
            'lead_id' => (string) $leadA->id,
            'limit' => 50,
        ])->assertRedirect();

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

        $this->assertNotNull(data_get($a->meta, 'subscription_activation.subscription_id'));
        $this->assertNull(data_get($b->meta, 'subscription_activation.subscription_id'));
    }
}
