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

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

        // 有回执：payment_receipts
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_EXPORT_RECEIPT_HAS_0001',
            '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' => [
                'payment_receipts' => [
                    ['amount' => 10.00, 'paid_at' => now()->toDateTimeString()],
                ],
            ],
        ]);

        // 有回执：payment_summary
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_EXPORT_RECEIPT_HAS_0002',
            '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' => [
                'payment_summary' => [
                    'count' => 1,
                    'total_amount' => 10.00,
                ],
            ],
        ]);

        // 无回执（广义）
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_EXPORT_RECEIPT_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&receipt_status=has');
        $res1->assertOk();
        $content1 = $res1->streamedContent();
        $this->assertStringContainsString('PO_EXPORT_RECEIPT_HAS_0001', $content1);
        $this->assertStringContainsString('PO_EXPORT_RECEIPT_HAS_0002', $content1);
        $this->assertStringNotContainsString('PO_EXPORT_RECEIPT_NONE_0003', $content1);

        $res2 = $this->get('/admin/platform-orders/export?download=1&receipt_status=none');
        $res2->assertOk();
        $content2 = $res2->streamedContent();
        $this->assertStringContainsString('PO_EXPORT_RECEIPT_NONE_0003', $content2);
        $this->assertStringNotContainsString('PO_EXPORT_RECEIPT_HAS_0001', $content2);
        $this->assertStringNotContainsString('PO_EXPORT_RECEIPT_HAS_0002', $content2);
    }
}
