<?php

namespace App\Http\Controllers;

use App\Mail\NovoLeadMail;
use App\Models\Lead;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{
    public function send(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'name'    => 'required|string|max:100',
            'email'   => 'required|email|max:150',
            'phone'   => 'nullable|string|max:30',
            'company' => 'nullable|string|max:150',
            'message' => 'required|string|max:2000',
        ]);

        $lead = Lead::create($validated);

        try {
            Mail::to('contato@opcon.com.br')->send(new NovoLeadMail($lead));
        } catch (\Throwable) {}

        return response()->json(['message' => 'Mensagem enviada com sucesso.']);
    }
}
