<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Models\PlatformLead;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class PlatformLeadController extends Controller
{
    public function store(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'name' => ['required', 'string', 'max:100'],
            'mobile' => ['nullable', 'string', 'max:30'],
            'email' => ['nullable', 'string', 'max:100'],
            'company' => ['nullable', 'string', 'max:100'],
            'plan_id' => ['nullable', 'integer', 'exists:plans,id'],
            'note' => ['nullable', 'string', 'max:2000'],
            'source' => ['nullable', 'string', 'max:50'],
        ]);

        PlatformLead::query()->create([
            'name' => (string) ($data['name'] ?? ''),
            'mobile' => (string) ($data['mobile'] ?? ''),
            'email' => (string) ($data['email'] ?? ''),
            'company' => (string) ($data['company'] ?? ''),
            'plan_id' => (int) ($data['plan_id'] ?? 0) ?: null,
            'note' => $data['note'] ?? null,
            'source' => (string) ($data['source'] ?? 'platform'),
            'status' => 'new',
            'meta' => [
                'ip' => $request->ip(),
                'user_agent' => (string) $request->userAgent(),
            ],
        ]);

        return redirect('/platform/plans')
            ->with('success', '已收到你的开通意向，我们会尽快联系你。');
    }
}
