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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'activate_btn_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_BTN_ACT_0001',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            'payment_status' => 'paid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'list_amount' => 66,
            'discount_amount' => 0,
            'payable_amount' => 66,
            'paid_amount' => 66,
            'placed_at' => now()->subMinutes(10),
            'paid_at' => now()->subMinutes(5),
            'activated_at' => now()->subMinutes(1),
            // 避免命中“治理优先”安全阀：构造回执与已付金额一致
            'meta' => [
                'payment_summary' => [
                    'count' => 1,
                    'total_amount' => 66.00,
                ],
            ],
        ]);

        $this->post('/admin/platform-orders/' . $order->id . '/activate-subscription')
            ->assertRedirect();

        $order->refresh();
        $this->assertNotNull($order->site_subscription_id);
    }

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'activate_btn_test_guest',
            '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_BTN_ACT_0002',
            '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()->subMinutes(10),
        ]);

        $this->post('/admin/platform-orders/' . $order->id . '/activate-subscription')
            ->assertStatus(302)
            ->assertRedirect('/admin/login');
    }
}
