<?php

namespace App\Http\Controllers;

use App\Mail\ContactFormMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{
    public function send(Request $request)
    {
        try {
            $validated = $request->validate([
                'name' => 'required|string|max:255',
                'email' => 'required|email|max:255',
                'phone' => 'required|string|max:50',
                'subject' => 'required|string|max:255',
                'message' => 'required|string|max:5000',
            ]);

            Mail::to(config('mail.from.address'))->send(new ContactFormMessage(
                senderName: $validated['name'],
                senderEmail: $validated['email'],
                senderPhone: $validated['phone'],
                messageSubject: $validated['subject'],
                messageBody: $validated['message'],
            ));

            return back()->with('success', 'Your message has been sent. We will get back to you shortly.');
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            return back()->with('alert', 'An error occurred.');
        }
    }
}
