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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'clear_sync_errors_refund_status_filter_fields_test',
            'name' => '清理失败标记退款筛选字段测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 1,
            'list_price' => 1,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // 无退款 + 有失败标记：应被 refund_status=none 命中并清理
        $noRefund = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_CLEAR_SYNC_ERRORS_REFUND_NONE_0001',
            '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' => 1,
            'paid_amount' => 0,
            'placed_at' => now(),
            'meta' => [
                'subscription_activation_error' => [
                    'message' => '模拟失败原因NONE',
                    'at' => now()->toDateTimeString(),
                    'admin_id' => 1,
                ],
            ],
        ]);

        // 有退款 + 有失败标记：不应被 refund_status=none 的清理误命中
        $hasRefund = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_CLEAR_SYNC_ERRORS_REFUND_HAS_0002',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 1,
            'paid_amount' => 1,
            'placed_at' => now(),
            'meta' => [
                'refund_summary' => [
                    'count' => 1,
                    'total_amount' => 0.50,
                ],
                'subscription_activation_error' => [
                    'message' => '模拟失败原因HAS',
                    'at' => now()->toDateTimeString(),
                    'admin_id' => 1,
                ],
            ],
        ]);

        // 访问列表页：确保清理失败标记表单透传 refund_status
        $page = $this->get('/admin/platform-orders?refund_status=none');
        $page->assertOk();
        $page->assertSee('name="refund_status"', false);
        $page->assertSee('value="none"', false);

        // 执行清理（filtered scope）
        $res = $this->post('/admin/platform-orders/clear-sync-errors', [
            'scope' => 'filtered',
            'refund_status' => 'none',
        ]);
        $res->assertRedirect();

        $noRefund->refresh();
        $hasRefund->refresh();

        $this->assertEmpty(data_get($noRefund->meta, 'subscription_activation_error'));
        $this->assertNotEmpty(data_get($hasRefund->meta, 'subscription_activation_error.message'));
    }
}
