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

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

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

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

        // A：待处理+未支付 + 归属 leadA（应被推进）
        $a = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BMPA_LEAD_A',
            '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),
            '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_BMPA_LEAD_B',
            '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' => [
                'platform_lead_id' => $leadB->id,
            ],
        ]);

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

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

        $this->assertSame('paid', $a->payment_status);
        $this->assertSame('activated', $a->status);
        $this->assertNotNull(data_get($a->meta, 'subscription_activation.subscription_id'));

        $this->assertSame('unpaid', $b->payment_status);
        $this->assertSame('pending', $b->status);
        $this->assertNull(data_get($b->meta, 'subscription_activation.subscription_id'));
    }
}
