<?php

namespace Tests\Feature;

use App\Models\Plan;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AdminPlanExportTest 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_admin_can_export_plans_csv(): void
    {
        $this->loginAsPlatformAdmin();

        Plan::query()->create([
            'code' => 'plan_export_01',
            'name' => '套餐导出测试',
            'billing_cycle' => 'monthly',
            'price' => 9,
            'list_price' => 19,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
            'description' => '用于导出断言',
        ]);

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

        $res->assertOk();
        $res->assertHeader('content-type', 'text/csv; charset=UTF-8');

        $content = $res->streamedContent();
        $this->assertStringContainsString('套餐名称', $content);
        $this->assertStringContainsString('套餐导出测试', $content);
        $this->assertStringContainsString('plan_export_01', $content);
    }

    public function test_guest_cannot_export_plans_csv(): void
    {
        $this->seed();

        $this->get('/admin/plans/export')
            ->assertRedirect('/admin/login');
    }
}
