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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'export_refund_status_filter_test',
            'name' => '导出退款筛选测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // 有退款：refund_receipts
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_EXPORT_REFUND_HAS_0001',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            'payment_status' => 'partially_refunded',
            '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' => [
                'refund_receipts' => [
                    ['amount' => 1.00, 'refunded_at' => now()->toDateTimeString()],
                ],
            ],
        ]);

        // 有退款：refund_summary
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_EXPORT_REFUND_HAS_0002',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            'payment_status' => 'refunded',
            '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' => [
                'refund_summary' => [
                    'count' => 1,
                    'total_amount' => 10.00,
                ],
            ],
        ]);

        // 无退款
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_EXPORT_REFUND_NONE_0003',
            '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' => [],
        ]);

        $res1 = $this->get('/admin/platform-orders/export?download=1&refund_status=has');
        $res1->assertOk();
        $content1 = $res1->streamedContent();
        $this->assertStringContainsString('PO_EXPORT_REFUND_HAS_0001', $content1);
        $this->assertStringContainsString('PO_EXPORT_REFUND_HAS_0002', $content1);
        $this->assertStringNotContainsString('PO_EXPORT_REFUND_NONE_0003', $content1);

        $res2 = $this->get('/admin/platform-orders/export?download=1&refund_status=none');
        $res2->assertOk();
        $content2 = $res2->streamedContent();
        $this->assertStringContainsString('PO_EXPORT_REFUND_NONE_0003', $content2);
        $this->assertStringNotContainsString('PO_EXPORT_REFUND_HAS_0001', $content2);
        $this->assertStringNotContainsString('PO_EXPORT_REFUND_HAS_0002', $content2);
    }
}
