<?php
namespace App\Controllers\Admin;
use App\Core\Service\Logger;
use App\Controllers\BaseController;
use App\Model\AdminModel;
use App\Service\Messaging;

class NotificationController extends BaseController
{
    private $AdminModel;
    private $Messaging;

    public function __construct() {
        $this->AdminModel = new AdminModel();
        $this->Messaging = new Messaging();
    }

    public function sendPendingNotification($id) {
        try {
            if($id > 0) {
                $resp = $this->AdminModel->getMasterData($this->AdminModel->tables['admin_email_sms_logs'], "id=:id", [":id"=>$id]);
                if(empty($resp['status'])) {
                    throw new \Exception($resp['error']);
                }
                if(empty($resp['data'])) {
                    throw new \Exception("Notification log not found");
                }
                if(strtoupper($resp['data']['type']) == 'EMAIL') {
                    $data = [
                        "subject"=>$resp['data']['email_subject'],
                        "body"=>$resp['data']['body'],
                        "to"=>explode(",",$resp['data']['email_to'])
                    ];
                    if(!empty($resp['data']['email_cc'])) {
                        $data['cc'] = explode(",",$resp['data']['email_cc']);
                    }
                    if(!empty($resp['data']['email_bcc'])) {
                        $data['bcc'] = explode(",",$resp['data']['email_bcc']);
                    }
                    $email_resp = $this->Messaging->sendAdminEmail($data);
                    $upd = [
                        "response" => !empty($email_resp['status']) ? addslashes($email_resp['data']) : addslashes($email_resp['error']),
                        "sent_status" => !empty($email_resp['status']) ? 1 : 2,
                        "sent_at"=> date("Y-m-d H:i:s")
                    ];
                    $upres = $this->AdminModel->updateMasterData('notification', $upd, ["id"=>$id]);
                    if(empty($upres['status'])) {
                        throw new \Exception($upres['error']);
                    }
                } else if(strtoupper($resp['data']['type']) == 'WHATSAPP') {
                    $data = [
                        "body"=>trim($resp['data']['body']),
                        "phone_no"=>trim($resp['data']['msg_to'])
                    ];
                    $wa_resp = $this->Messaging->sendAdminWhatsApp($data);
                    $upd = [
                        "response" => !empty($wa_resp['status']) ? addslashes(\json_encode($wa_resp['data'])) : addslashes($wa_resp['error']),
                        "sent_status" => !empty($wa_resp['status']) ? 1 : 2,
                        "sent_at"=> date("Y-m-d H:i:s")
                    ];
                    $upres = $this->AdminModel->updateMasterData('notification', $upd, ["id"=>$id]);
                    if(empty($upres['status'])) {
                        throw new \Exception($upres['error']);
                    }
                } else if(strtoupper($resp['data']['type']) == 'SMS') {
                    $data = [
                        "body"=>trim($resp['data']['body']),
                        "phone_no"=>trim($resp['data']['msg_to'])
                    ];
                    $sms_resp = $this->Messaging->sendAdminMessage($data);
                    $upd = [
                        "response" => !empty($sms_resp['status']) ? addslashes(\json_encode($sms_resp['data'])) : addslashes($sms_resp['error']),
                        "sent_status" => !empty($sms_resp['status']) ? 1 : 2,
                        "sent_at"=> date("Y-m-d H:i:s")
                    ];
                    $upres = $this->AdminModel->updateMasterData('notification', $upd, ["id"=>$id]);
                    if(empty($upres['status'])) {
                        throw new \Exception($upres['error']);
                    }
                }
                echo "Notification executed\r\n";
            }
        } catch(\Exception $e) {
            Logger::error($e->getMessage(), ['File' => $e->getFile(), 'Line'=>$e->getLine()]);
            echo $e->getMessage();exit;
        }

    }
}