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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'clear_sync_batch_bmpa_24h_plan',
            'name' => '清理同步失败标记 24h批量BMPA筛选透传 测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $recent = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_CLEAR_SYNC_24H_RECENT',
            'order_type' => 'renewal',
            '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' => [
                'batch_mark_paid_and_activate' => [
                    'at' => now()->toDateTimeString(),
                    'admin_id' => 1,
                    'scope' => 'filtered',
                ],
                'subscription_activation_error' => [
                    'message' => '模拟同步失败（recent）',
                    'at' => now()->toDateTimeString(),
                    'admin_id' => 1,
                ],
            ],
        ]);

        $old = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_CLEAR_SYNC_24H_OLD',
            'order_type' => 'renewal',
            '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()->subHours(30),
            'paid_at' => now()->subHours(30),
            'activated_at' => now()->subHours(30),
            'meta' => [
                'batch_mark_paid_and_activate' => [
                    'at' => now()->subHours(30)->toDateTimeString(),
                    'admin_id' => 1,
                    'scope' => 'filtered',
                ],
                'subscription_activation_error' => [
                    'message' => '模拟同步失败（old）',
                    'at' => now()->subHours(30)->toDateTimeString(),
                    'admin_id' => 1,
                ],
            ],
        ]);

        $this->post('/admin/platform-orders/clear-sync-errors', [
            'scope' => 'filtered',
            'batch_mark_paid_and_activate_24h' => '1',
        ])->assertRedirect();

        $recent->refresh();
        $old->refresh();

        $this->assertNull(data_get($recent->meta, 'subscription_activation_error'));
        $this->assertNotNull(data_get($old->meta, 'subscription_activation_error'));
    }
}
