<?php

namespace App\Services;

use App\Mail\ContactFormSubmitted;
use App\Models\ContactSubmission;
use Illuminate\Support\Facades\Mail;

class ContactSubmissionService
{
    public function store(array $data, ?string $ipAddress = null): ContactSubmission
    {
        $submission = ContactSubmission::query()->create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'] ?? null,
            'email' => $data['email'],
            'phone' => $data['phone'] ?? null,
            'interest' => $data['interest'],
            'message' => $data['message'],
            'ip_address' => $ipAddress,
        ]);

        Mail::to(config('mail.contact_recipient'))
            ->queue(new ContactFormSubmitted($submission));

        return $submission;
    }
}
