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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'activate_error_meta_test',
            'name' => '同步失败记录测试',
            'billing_cycle' => 'monthly',
            'price' => 9,
            'list_price' => 9,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // 构造一个“不满足 paid+activated”的订单，触发失败
        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_ACT_ERR_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 9,
            'paid_amount' => 0,
            'placed_at' => now(),
        ]);

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

        $order->refresh();
        $this->assertNotEmpty(data_get($order->meta, 'subscription_activation_error.message'));
        $this->assertNotEmpty(data_get($order->meta, 'subscription_activation_error.at'));
    }

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'activate_error_meta_test2',
            'name' => '同步成功清理失败记录测试',
            'billing_cycle' => 'monthly',
            'price' => 9,
            'list_price' => 9,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_ACT_ERR_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' => 9,
            'paid_amount' => 9,
            'placed_at' => now(),
            'paid_at' => now(),
            'activated_at' => now(),
            'meta' => [
                'subscription_activation_error' => [
                    'message' => '历史错误',
                    'at' => now()->subDay()->toDateTimeString(),
                    'admin_id' => 1,
                ],
            ],
        ]);

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

        $order->refresh();
        $this->assertEmpty(data_get($order->meta, 'subscription_activation_error'));
        $this->assertNotNull($order->site_subscription_id);
    }
}
