<?php

namespace Tests\Feature;

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

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

        $plan = Plan::query()->create([
            'code' => 'plan_index_hide_create_order_when_not_active',
            'name' => '套餐页隐藏创建订单（未启用）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 99,
            'list_price' => 99,
            // 非 active
            'status' => 'draft',
            'sort' => 1,
            'published_at' => null,
        ]);

        $res = $this->get('/admin/plans');
        $res->assertOk();

        $html = (string) $res->getContent();

        // 该套餐行内不应出现“创建订单”入口（避免对未启用套餐误下单）
        $this->assertStringNotContainsString('/admin/platform-orders/create?plan_id=' . $plan->id, $html);
        $this->assertStringContainsString('未启用：不建议下单', $html);
    }
}
