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

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

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_EXPORT_REFUND_FIELDS_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' => [
                    [
                        'type' => 'refund',
                        'channel' => 'alipay',
                        'amount' => 1.23,
                        'refunded_at' => now()->subDay()->format('Y-m-d H:i:s'),
                        'note' => '第一次退款',
                    ],
                    [
                        'type' => 'refund',
                        'channel' => 'wechat',
                        'amount' => 4.56,
                        'refunded_at' => now()->format('Y-m-d H:i:s'),
                        'note' => '第二次退款',
                    ],
                ],
                'refund_summary' => [
                    'count' => 2,
                    'total_amount' => 5.79,
                ],
            ],
        ]);

        $res = $this->get('/admin/platform-orders/export?download=1');
        $res->assertOk();

        $content = $res->streamedContent();

        // 关键字段存在
        $this->assertStringContainsString('退款记录数', $content);
        $this->assertStringContainsString('最近退款时间', $content);
        $this->assertStringContainsString('最近退款金额', $content);
        $this->assertStringContainsString('最近退款渠道', $content);
        $this->assertStringContainsString('退款总额', $content);

        // 行数据存在
        $this->assertStringContainsString('PO_EXPORT_REFUND_FIELDS_0001', $content);
        $this->assertStringContainsString('wechat', $content);
        $this->assertStringContainsString('5.79', $content);
        $this->assertStringContainsString('4.56', $content);
    }
}
