<?php

namespace App\Utility;

use App\Jobs\SendEmailJob;
use App\Mail\SendEmail;
use Illuminate\Support\Facades\Mail;
use App\Models\User;

class Email
{
    private bool $is_queue_enable = false;

    private  array $result = [];
    private array $logData = [];

    public function __construct()
    {
        $this->is_queue_enable = config('app.IS_QUEUE_ENABLE');
    }

    public function send($data, $email_subject, $from, $attachment, $template, $to)
    {
        if ($this->is_queue_enable == 1) {
            dispatch((new SendEmailJob($data, $email_subject, $from, $attachment, $template, $to)));
        } else {
            $this->sendEmailManually($data, $email_subject, $from, $attachment, $template, $to);
        }
    }

    private function sendEmailManually($data, $email_subject, $from, $attachment, $template, $to) : void
    {
        try{
            if(empty($to)){
                throw new \Exception("Recipient email is missing.");
            }

            Mail::to($to)->send(
                new SendEmail(
                    $data,
                    $email_subject,
                    $from,
                    $attachment,
                    $template
                )
            );
            $this->result['message'] = 'E-Mail has been send successfully.';
            $this->result['status'] = true;

            @unlink($attachment);

        }catch (\Throwable $ex){
            $this->result['message'] = $ex->getMessage();
            $this->result['status'] = false;
        }

        $this->logData['SEND_EMAIL'] = $this->prepareLogData($from,$to,$email_subject);
        appLog($this->logData);
    }

    public function clientConfirmationMail($inputData):void
    {
        $data['email'] = $inputData['email'];
        $data['email_body'] = $inputData['name'];
        $data['email_subject'] = "Congratulations! You are In...";
        $data['password'] = $inputData['password'];
        $data['url'] = config('app.url');

        $from = null;
        $attachment = null;

        (new Email())->send($data, $data['email_subject'], $from, $attachment, 'email.client_confirmation_letter', $data['email']);

    }

    public function clientRegistrationMail($inputData):void
    {
        $data['email'] = $inputData['email'];
        $data['email_body'] = $inputData['name'];
        $data['email_subject'] = "You Have A New CCE Signup!";

        $from = null;
        $attachment = null;

        $result = (new User())->getAdminEmployee();

        $admin_email = $result->where('user_type',1)->first();

        $employee_email = $result->where('user_type',2)->first();

        $admin_email = $admin_email ? $admin_email['email']: config('app.DEFAULT_ADMIN');
        $employee_email = $employee_email ? $employee_email['email'] : config('app.DEFAULT_EMPLOYEE');


        (new Email())->send($data, $data['email_subject'], $from, $attachment, 'email.account_registration_letter', $admin_email);

        (new Email())->send($data, $data['email_subject'], $from, $attachment, 'email.account_registration_letter', $employee_email);

        $data['email_subject'] = "Congratulations! Your Almost there.";
        (new Email())->send($data, $data['email_subject'], $from, $attachment, 'email.account_registration_client_letter', $data['email']);

    }
    private function prepareLogData($from_email,$to,$email_subject){
        return [
            'sending_info' => [
                'from' => $from_email,
                'to' => $to,
                'subject' => $email_subject
            ],
            'response' => $this->result
        ];
    }
}
