<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\Admin;
use App\Rules\Captcha;
use App\Models\Setting;
use App\EmailConfiguration;
use App\Helpers\MailHelper;
use Illuminate\Http\Request;
use App\Models\EmailTemplate;
use App\Models\ContactMessage;
use App\Models\ValidationText;
use App\Models\NotificationText;

use Illuminate\Support\Facades\Mail;
use App\Mail\ContactMessageInformation;
use Exception;
use PhpParser\Node\Stmt\TryCatch;

class ContactController extends Controller
{

    public function sendMessage(Request $request){

        $rules = [
            'name'=>'required',
            'email'=>'required|email',
            'subject'=>'required',
            'message'=>'required',
            'g-recaptcha-response'=>new Captcha()
        ];
        $customMessages = [
            'name.required' => trans('user_validation.Name is required'),
            'email.required' => trans('user_validation.Email is required'),
            'subject.required' => trans('user_validation.Subject is required'),
            'message.required' => trans('user_validation.Message is Required'),
        ];
        $this->validate($request, $rules, $customMessages);

        $contact=[
            'email'=>$request->email,
            'phone'=>$request->phone,
            'name'=>$request->name,
            'subject'=>$request->subject,
            'message'=>$request->message,
        ];

        $setting=Setting::first();
        if($setting->enable_save_contact_message==1){
            $contact = new ContactMessage();
            $contact->email = $request->email;
            $contact->phone = $request->phone;
            $contact->name = $request->name;
            $contact->message = $request->message;
            $contact->subject = $request->subject;
            $contact->save();
        }

        MailHelper::setMailConfig();

        $template=EmailTemplate::where('id',2)->first();
        $message=$template->description;
        $subject=$template->subject;
        $message=str_replace('{{name}}',$contact['name'],$message);
        $message=str_replace('{{email}}',$contact['email'],$message);
        $message=str_replace('{{phone}}',$contact['phone'],$message);
        $message=str_replace('{{subject}}',$contact['subject'],$message);
        $message=str_replace('{{message}}',$contact['message'],$message);
        try{
            if(Mail::to($setting->contact_email)->send(new ContactMessageInformation($message,$subject))){
                $notification = trans('user_validation.Message send successfully');
                $notification=array('messege'=>$notification,'alert-type'=>'success');
            }else{
                $notification = trans('user_validation.Failed to send message');
                $notification=array('messege'=>$notification,'alert-type'=>'error');
            }
        }catch(Exception $e){
            $notification = trans('user_validation.The message could not be sent due to invalid SMTP credentials');
            $notification=array('messege'=>$notification,'alert-type'=>'error');
        }
        return back()->with($notification);
    }

    public function messageForUser(Request $request){

        $rules = [
            'name'=>'required',
            'email'=>'required|email',
            'subject'=>'required',
            'message'=>'required',
            'user_type'=>'required',
            'g-recaptcha-response'=>new Captcha()
        ];
        $customMessages = [
            'name.required' => trans('user_validation.Name is required'),
            'email.required' => trans('user_validation.Email is required'),
            'subject.required' => trans('user_validation.Subject is required'),
            'message.required' => trans('user_validation.Message is Required'),
        ];
        $this->validate($request, $rules, $customMessages);


        $contact=[
            'email'=>$request->email,
            'phone'=>$request->phone,
            'name'=>$request->name,
            'subject'=>$request->subject,
            'message'=>$request->message,
        ];


        MailHelper::setMailConfig();

        if($request->user_type==1){
            $admin=Admin::find($request->admin_id);
            if($admin){
                $template=EmailTemplate::where('id',2)->first();
                $message=$template->description;
                $subject=$template->subject;
                $message=str_replace('{{name}}',$contact['name'],$message);
                $message=str_replace('{{email}}',$contact['email'],$message);
                $message=str_replace('{{phone}}',$contact['phone'],$message);
                $message=str_replace('{{subject}}',$contact['subject'],$message);
                $message=str_replace('{{message}}',$contact['message'],$message);

                Mail::to($admin->email)->send(new ContactMessageInformation($message,$subject));

                $notification = trans('user_validation.Message send successfully');
                return response()->json(['success'=>$notification]);

            }else{
                $notification= trans('user_validation.Something Went Wrong');
                return response()->json(['error'=>$notification]);
            }
        }else if($request->user_type==0 || $request->user_type==2){
            $user=User::find($request->user_id);
            if($user){
                $template=EmailTemplate::where('id',2)->first();
                $message=$template->description;
                $subject=$template->subject;
                $message=str_replace('{{name}}',$contact['name'],$message);
                $message=str_replace('{{email}}',$contact['email'],$message);
                $message=str_replace('{{phone}}',$contact['phone'],$message);
                $message=str_replace('{{subject}}',$contact['subject'],$message);
                $message=str_replace('{{message}}',$contact['message'],$message);

                Mail::to($user->email)->send(new ContactMessageInformation($message,$subject));

                $notification = trans('user_validation.Message send successfully');
                return response()->json(['success'=>$notification]);

            }else{
                $notification= trans('user_validation.Something Went Wrong');
                return response()->json(['error'=>$notification]);
            }
        }else{
            $notification= trans('user_validation.Something Went Wrong');
            return response()->json(['error'=>$notification]);
        }
    }
}
