<?php

namespace Tests\Feature;

use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformLead;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AdminPlatformOrderStoreFromLeadShouldMarkLeadConvertedTest 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_store_with_lead_id_should_mark_lead_as_converted_and_record_meta(): void
    {
        $this->loginAsPlatformAdmin();

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

        $plan = Plan::query()->create([
            'code' => 'lead_order_link_plan',
            'name' => '线索联动下单测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $lead = PlatformLead::query()->create([
            'name' => '联动线索',
            'mobile' => '13800000003',
            'email' => 'lead3@example.com',
            'company' => '联动公司',
            'source' => 'test',
            'status' => 'new',
            'plan_id' => $plan->id,
            'meta' => ['from' => 'test'],
        ]);

        $res = $this->post('/admin/platform-orders', [
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_type' => 'new_purchase',
            'quantity' => 1,
            'discount_amount' => 0,
            'payment_channel' => 'manual',
            'remark' => 'from lead',
            'lead_id' => $lead->id,
            'back' => '/admin/platform-leads',
        ]);

        $res->assertRedirect();

        $lead->refresh();
        $this->assertSame('converted', $lead->status);

        $order = \App\Models\PlatformOrder::query()->latest('id')->firstOrFail();
        $this->assertSame($merchant->id, $order->merchant_id);
        $this->assertSame($plan->id, $order->plan_id);
        $this->assertSame($lead->id, (int) data_get($order->meta, 'platform_lead_id'));
    }

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

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

        $plan = Plan::query()->create([
            'code' => 'lead_order_link_plan2',
            'name' => '线索联动下单测试套餐2',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $lead = PlatformLead::query()->create([
            'name' => '已关闭线索',
            'mobile' => '',
            'email' => '',
            'company' => '',
            'source' => 'test',
            'status' => 'closed',
            'plan_id' => $plan->id,
            'meta' => ['from' => 'test'],
        ]);

        $res = $this->post('/admin/platform-orders', [
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_type' => 'new_purchase',
            'quantity' => 1,
            'discount_amount' => 0,
            'payment_channel' => 'manual',
            'remark' => 'from closed lead',
            'lead_id' => $lead->id,
        ]);

        $res->assertRedirect();

        $this->assertSame('closed', $lead->fresh()->status);
    }
}
