<?php

namespace Tests\Feature;

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

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'po_create_renewal_gov_plan',
            'name' => '创建订单续费治理提示测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $sub = SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_RENEW_GOV_0001',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 10,
            'starts_at' => now()->subDay(),
            'ends_at' => now()->addDays(10),
            'activated_at' => now()->subDay(),
        ]);

        $url = '/admin/platform-orders/create?site_subscription_id=' . $sub->id . '&order_type=renewal';

        $res = $this->get($url);
        $res->assertOk();

        $res->assertSee('续费对象确认', false);
        $res->assertSee('SUB_RENEW_GOV_0001', false);
        $res->assertSee('订阅ID：' . $sub->id, false);

        // 使用统一的治理提示块样式类（避免 inline style 漂移）
        $res->assertSee('class="governance-block', false);
    }

    public function test_create_form_should_not_render_governance_block_when_order_type_is_not_renewal(): void
    {
        $this->loginAsPlatformAdmin();

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'po_create_non_renewal_gov_plan',
            'name' => '创建订单非续费不展示治理提示测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $sub = SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_RENEW_GOV_0002',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 10,
            'starts_at' => now()->subDay(),
            'ends_at' => now()->addDays(10),
            'activated_at' => now()->subDay(),
        ]);

        $url = '/admin/platform-orders/create?site_subscription_id=' . $sub->id . '&order_type=new_purchase';

        $res = $this->get($url);
        $res->assertOk();

        $res->assertDontSee('续费对象确认', false);
        $res->assertDontSee('class="governance-block', false);
    }
}
