<?php

namespace App\Http\Controllers\FrontControllers;

use App\Http\Controllers\Controller;
use App\Mail\ContactNotification;
use App\Models\Setting;
use App\Repositories\CMSPagesRepositoryEloquent;
use Illuminate\Http\Request;
use App\Traits\PHPCustomMail;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{
    use PHPCustomMail;
    private $cmsPageRepository;

    public function __construct(
        CMSPagesRepositoryEloquent $cMSPagesRepository,
    ) {
        $this->cmsPageRepository = $cMSPagesRepository;
    }

    public function index()
    {
        $data = $this->cmsPageRepository->getPageBySlug('contact');
        return view('front.contact', compact('data'));
    }

    public function submitContactForm(Request $request)
    {
        $request->validate([
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required|email',
            'message' => 'required',
        ]);

        try {
            $setting = Setting::first();
            $data = [
                'full_name' => $request->first_name . ' ' . $request->last_name,
                'email' => $request->email,
                'message' => $request->message,
            ];

            // Send the email
            Mail::send('user.email.contact-form', ['data' => $data], function ($message) use ($setting, $data) {
                $message->to($setting->email, $data['full_name'])
                    ->subject('Contact Us')
                    ->from('no-reply@luxury-candles.com', 'Luxury Candles');
            });
            //            Mail::to('info@luxury-candles.net')->send(new ContactNotification($data));
            // $view = view('user.email.contact-form', compact(['email', 'full_name', 'message']))->render();
            // $data = [
            //     'from' => $email,
            //     'to' => 'ajyaror88@yahoo.com',
            //     'subject' => 'Contact Information',
            //     'body' => $view,
            // ];
            // $this->customMail($data['from'], $data['to'], $data['subject'], $data['body']);

            return redirect()->back()->with('success', 'Form Submit Successfully');
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }
    }
}
