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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'show_activate_gov_hint_reconcile_test',
            'name' => '详情页同步订阅提示（对账不一致）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 66,
            'list_price' => 66,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_SHOW_ACT_GOV_HINT_RECON_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' => 66,
            'paid_amount' => 66,
            'placed_at' => now(),
            'paid_at' => now(),
            'activated_at' => now(),
            'meta' => [
                'payment_summary' => [
                    'count' => 1,
                    'total_amount' => 65.98,
                ],
            ],
        ]);

        $this->get('/admin/platform-orders/' . $order->id)
            ->assertOk()
            ->assertSee('当前订单命中「对账不一致/退款不一致」', false)
            ->assertSee('#add-payment-receipt', false);
    }

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'show_activate_gov_hint_refund_test',
            'name' => '详情页同步订阅提示（退款不一致）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 66,
            'list_price' => 66,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_SHOW_ACT_GOV_HINT_REFUND_0002',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            'payment_status' => 'refunded',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 66,
            'paid_amount' => 66,
            'placed_at' => now(),
            'paid_at' => now(),
            'activated_at' => now(),
            'refunded_at' => now(),
            'meta' => [
                'refund_summary' => [
                    'count' => 1,
                    'total_amount' => 65.98,
                ],
            ],
        ]);

        $this->get('/admin/platform-orders/' . $order->id)
            ->assertOk()
            ->assertSee('当前订单命中「对账不一致/退款不一致」', false)
            ->assertSee('#add-refund-receipt', false);
    }
}
