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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'show_order_test',
            '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_SHOW_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' => 10,
            'paid_amount' => 0,
            'placed_at' => now(),
            'meta' => [
                'subscription_activation_error' => [
                    'message' => 'demo',
                    'at' => now()->toDateTimeString(),
                ],
                'audit' => [
                    [
                        'action' => 'clear_sync_error',
                        'scope' => 'filtered',
                        'at' => now()->toDateTimeString(),
                        'admin_id' => 1,
                    ],
                    [
                        'action' => 'batch_activate_subscription',
                        'scope' => 'filtered',
                        'at' => now()->toDateTimeString(),
                        'admin_id' => 1,
                    ],
                ],
            ],
        ]);

        // 关联一个订阅，确保详情页出现“打开订阅详情”入口
        $sub = \App\Models\SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_SHOW_0001',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 10,
            'starts_at' => now()->subDay(),
            'ends_at' => now()->addMonth(),
            'activated_at' => now()->subDay(),
        ]);

        $order->site_subscription_id = $sub->id;
        $order->save();

        $this->get('/admin/platform-orders/' . $order->id)
            ->assertOk()
            ->assertSee('平台订单详情')
            ->assertSee('PO_SHOW_0001')
            ->assertSee('标记支付并生效')
            ->assertSee('同步订阅')
            ->assertSee('查看已同步订阅')
            ->assertSee('最近批量生效记录')
            ->assertSee('订阅同步元数据')
            ->assertSee('退款时间')
            ->assertSee('关联订阅')
            ->assertSee('打开订阅详情')
            ->assertSee('/admin/site-subscriptions/' . $sub->id . '?back=' . urlencode('/admin/platform-orders/' . $order->id), false)
            ->assertSee('order_sync_status=syncable')
            ->assertSee('#syncable-batch')
            ->assertSee('审计记录')
            ->assertSee('清除同步失败标记');
    }

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'show_order_test_guest',
            'name' => '订单详情测试(guest)',
            '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_SHOW_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(),
        ]);

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