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

        // 需要至少一个套餐供选择
        Plan::query()->create([
            'code' => 'create_order_plan_01',
            'name' => '创建订单测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 100,
            'list_price' => 100,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $res = $this->get('/admin/platform-orders/create');

        $res->assertOk();
        $res->assertSee('新建平台订单');
        $res->assertSee('站点');
        $res->assertSee('套餐');
        $res->assertSee('创建订单');
    }

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

        // 需要至少一个套餐供选择
        Plan::query()->create([
            'code' => 'create_order_plan_fields_01',
            'name' => '创建订单字段渲染测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 100,
            'list_price' => 100,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $res = $this->get('/admin/platform-orders/create');

        $res->assertOk();

        // Guardrails: 页面必须渲染出核心表单字段，否则运营无法下单。
        $res->assertSee('form-grid', false);
        $res->assertSee('name="merchant_id"', false);
        $res->assertSee('name="plan_id"', false);
        $res->assertSee('name="order_type"', false);
        $res->assertSee('name="quantity"', false);
        $res->assertSee('创建订单');
    }

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

        $merchant = Merchant::query()->firstOrFail();

        $plan = Plan::query()->create([
            'code' => 'create_order_plan_01b',
            'name' => '创建订单测试套餐(订阅联动)',
            'billing_cycle' => 'monthly',
            'price' => 100,
            'list_price' => 100,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $sub = \App\Models\SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_CREATE_ORDER_HINT_0001',
            'plan_name' => $plan->name,
            'billing_cycle' => 'monthly',
            'period_months' => 1,
            'amount' => 88,
            'starts_at' => now()->subDays(2),
            'ends_at' => now()->addDays(20),
            'activated_at' => now()->subDays(2),
        ]);

        $res = $this->get('/admin/platform-orders/create?site_subscription_id=' . $sub->id);

        $res->assertOk();
        $res->assertSee('本订单将关联订阅：');
        $res->assertSee('SUB_CREATE_ORDER_HINT_0001');
        $res->assertSee('/admin/site-subscriptions/' . $sub->id);
    }

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

        $merchant = Merchant::query()->firstOrFail();

        $plan = Plan::query()->create([
            'code' => 'create_order_plan_02',
            'name' => '创建订单测试套餐2',
            'billing_cycle' => 'monthly',
            'price' => 199,
            'list_price' => 199,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $res = $this->post('/admin/platform-orders', [
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_type' => 'new_purchase',
            'quantity' => 2,
            'discount_amount' => 10,
            'payment_channel' => 'offline',
            'remark' => '线下补单',
        ]);

        $res->assertRedirect();

        $order = PlatformOrder::query()->latest('id')->first();
        $this->assertNotNull($order);
        $this->assertSame($merchant->id, $order->merchant_id);
        $this->assertSame($plan->id, $order->plan_id);
        $this->assertSame('pending', $order->status);
        $this->assertSame('unpaid', $order->payment_status);
        $this->assertSame('offline', $order->payment_channel);

        // 金额口径：list_amount=price*quantity，payable=list-discount
        $this->assertSame(398.0, (float) $order->list_amount);
        $this->assertSame(10.0, (float) $order->discount_amount);
        $this->assertSame(388.0, (float) $order->payable_amount);

        $show = $this->get('/admin/platform-orders/' . $order->id);
        $show->assertOk();
        $show->assertSee($order->order_no);
        $show->assertSee('平台订单详情');
        $show->assertSee('待处理');
        $show->assertSee('未支付');
    }

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

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