<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{
    public function submit(Request $request)
    {
        $request->validate([
            'full_name' => 'required|string|max:255',
            'email_address' => 'required|email',
            'phone_number' => 'nullable|string',
            'subject' => 'required|string',
            'message' => 'required|string',
        ]);

        // Collect form data
        $data = $request->all();
        
        // Send email (you might want to use Laravel Mail instead)
        $to = "info@digitalstudio.co.uk";
        $subject = "New Message from: " . $data['full_name'];
        $email_body = "Full Name: {$data['full_name']}\n";
        $email_body .= "Email Address: {$data['email_address']}\n";
        $email_body .= "Phone Number: {$data['phone_number']}\n";
        $email_body .= "Subject: {$data['subject']}\n\n";
        $email_body .= "Message:\n{$data['message']}\n";
        
        $headers = "From: {$data['email_address']}\r\n";
        $headers .= "Reply-To: {$data['email_address']}\r\n";
        $headers .= "X-Mailer: PHP/" . phpversion();
        
        if (mail($to, $subject, $email_body, $headers)) {
            return back()->with('success', 'Your message has been sent successfully!');
        } else {
            return back()->with('error', 'Failed to send your message. Please try again.');
        }
    }
}