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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'mark_activated_plan',
            'name' => '标记生效测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_MARK_ACTIVATED_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 10,
            'placed_at' => now()->subMinutes(10),
            'paid_at' => now()->subMinutes(5),
            'meta' => [],
        ]);

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

        $order->refresh();
        $this->assertSame('activated', $order->status);
        $this->assertSame('paid', $order->payment_status);
        $this->assertNotNull($order->activated_at);

        $audit = (array) (data_get($order->meta, 'audit', []) ?? []);
        $this->assertNotEmpty($audit);
        $this->assertSame('mark_activated', (string) data_get(end($audit), 'action'));
    }

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'mark_activated_plan2',
            'name' => '标记生效测试套餐2',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_MARK_ACTIVATED_0002',
            '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' => 10,
            'paid_amount' => 0,
            'placed_at' => now()->subMinutes(10),
        ]);

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

        $order->refresh();
        $this->assertSame('pending', $order->status);
        $this->assertSame('unpaid', $order->payment_status);
    }

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'mark_activated_plan3',
            'name' => '标记生效测试套餐3',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_MARK_ACTIVATED_0003',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 10,
            'placed_at' => now()->subMinutes(10),
            'paid_at' => now()->subMinutes(5),
        ]);

        $this->post('/admin/platform-orders/' . $order->id . '/mark-activated')
            ->assertRedirect('/admin/login');
    }
}
