<?php

namespace App\Jobs;

use App\Models\ContactInquiry;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Mail;

class ProcessContactInquiryJob implements ShouldQueue
{
    use Queueable;

    public function __construct(public string $inquiryId)
    {
    }

    public function handle(): void
    {
        $inquiry = ContactInquiry::find($this->inquiryId);
        if (!$inquiry) {
            return;
        }

        $recipient = config('mail.from.address', 'support@quickropos.com');

        Mail::raw(
            "New Quickro POS inquiry\n\nName: {$inquiry->name}\nEmail: {$inquiry->email}\nBusiness: {$inquiry->business_name}\nPhone: {$inquiry->phone}\n\n{$inquiry->message}",
            function ($message) use ($recipient, $inquiry) {
                $message->to($recipient)
                    ->replyTo($inquiry->email, $inquiry->name)
                    ->subject('Quickro POS inquiry from ' . $inquiry->name);
            }
        );

        $inquiry->update([
            'status' => 'sent',
            'delivery_notes' => 'Queued mail notification sent.',
        ]);
    }
}
