<?php

namespace Tests\Feature;

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

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

        $lead = PlatformLead::query()->create([
            'name' => '测试线索',
            'mobile' => '13800000000',
            'email' => 't@example.com',
            'company' => '测试公司',
            'source' => 'test',
            'status' => 'new',
            'plan_id' => null,
            'meta' => ['from' => 'test'],
        ]);

        $res = $this->post('/admin/platform-leads/' . $lead->id . '/set-status', [
            'status' => 'contacted',
        ]);

        $res->assertRedirect();
        $this->assertSame('contacted', $lead->fresh()->status);
    }

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

        $lead = PlatformLead::query()->create([
            'name' => '测试线索2',
            'mobile' => '13800000001',
            'email' => 't2@example.com',
            'company' => '测试公司2',
            'source' => 'test',
            'status' => 'new',
            'plan_id' => null,
            'meta' => ['from' => 'test'],
        ]);

        $res = $this->from('/admin/platform-leads')->post('/admin/platform-leads/' . $lead->id . '/set-status', [
            'status' => 'hacked_status',
        ]);

        $res->assertRedirect('/admin/platform-leads');
        $res->assertSessionHasErrors('status');
        $this->assertSame('new', $lead->fresh()->status);
    }
}
