<?php

namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use App\Models\AgentModel;

class AgentAuthController extends ResourceController
{
    protected $format = 'json';
    protected $myLogger;
    protected $AgentModel;


    public function __construct()
    {
        helper('jwt_helper');
        helper('oauth_helper');
        $this->myLogger = \Config\Services::mylogger();
        $this->AgentModel = new AgentModel();

    }

    public function verifyAgentWithMobileNumber()
    {   
        try {

            $mobile = $this->request->getJSON()->mobile;
         
            $agentData = $this->AgentModel->where('mobile', $mobile)->where('is_active', 1)->first();

           
            if (isset($agentData['id'])) 
            {

                if (env('CI_ENVIRONMENT') === 'development') {
                    $otp = 123456; // Static OTP for development
                } else {
                    $otp = random_int(100000, 999999);
                }

                $update = $this->AgentModel->where('mobile', $mobile)->where('is_active', 1)->set(['email_otp' => $otp])->update();
                    
                if ($update) {

                    //  Skip SMS in development
                    if (env('CI_ENVIRONMENT') === 'development') {
                        $result = ['agent_verification' => true, 'message' => "Verified Successfully (DEV MODE - OTP: 123456)" ];
                        return $this->respond(['status' => 'success','code'   => 200,'data'   => $result], 200);
                    }
     
                    $SMSResult = sendOtpSms($mobile,$otp);

                    if ($SMSResult['status'] == 'success') {
                        $result = ['agent_verification' => true, 'message' => "Verified Successfully"];
                        return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result], 200);
                    } else {
                        $result = ['agent_verification' => false, 'message' => "Mail sending failed , try again"];
                        return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
                    }
                } else {
                    $result = ['agent_verification' => false, 'message' => "Verification failed , try again"];
                    return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
                }

            } else {
               
                $result = ['agent_verification' => false , 'message' => "User not found"];
                return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);

            }
        } catch (\Throwable $th) {
           
            return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
        }
    }

    public function verifyAgentWithEmailId()
    {   
        try {

            $email = $this->request->getJSON()->email;

            $agentData = $this->AgentModel->where('email', $email)->where('is_active', 1)->first();

            if (isset($agentData['id'])) {

                if (env('CI_ENVIRONMENT') === 'development') {
                    $otp = 123456; // Static OTP for development
                } else {
                    $otp = random_int(100000, 999999);
                }

                $update = $this->AgentModel->where('email', $email)->where('is_active', 1)->set(['email_otp' => $otp])->update();
                    
                if ($update) {

                    //  Skip SMS in development
                    if (env('CI_ENVIRONMENT') === 'development') {
                        $result = ['agent_verification' => true, 'message' => "Verified Successfully (DEV MODE - OTP: 123456)" ];
                        return $this->respond(['status' => 'success','code'   => 200,'data'   => $result], 200);
                    }
     
                    $EmailResult = trigger_email('login_otp',['email'=>$email, 'name'=>$agentData['name'], 'otp'=>$otp, ]);

                    if ($EmailResult['status'] == 'success') {
                        $result = ['agent_verification' => true, 'message' => "Verified Successfully"];
                        return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result], 200);
                    } else {
                        $result = ['agent_verification' => false, 'message' => "Mail sending failed , try again"];
                        return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
                    }
                } else {
                    $result = ['agent_verification' => false, 'message' => "Verification failed , try again"];
                    return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
                }

            } else {
                $result = ['agent_verification' => false, 'message' => "User not found"];
                return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
            }

        } catch (\Throwable $th) {
            return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $th], 500);
        }
    }

    public function getVerifiedAgentData()
    {   
        try {

            $mobile = isset($this->request->getJSON()->mobile) ? $this->request->getJSON()->mobile : null;
            $email = isset($this->request->getJSON()->email) ? $this->request->getJSON()->email : null;
            $otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null;


            if (isset($mobile)) 
            {
                $agentData = $this->AgentModel->where('mobile', $mobile)->where('is_active', 1)->where('email_otp', $otp)->first();

            } else {
                $agentData = $this->AgentModel->where('email', $email)->where('email_otp', $otp)->where('is_active', 1)->first();
            }   
            

            if ($agentData && isset($this->request->getJSON()->otp) ) {
               
                $this->AgentModel->where('id', $agentData['id'])->where('email_otp', $otp)->where('is_active', 1)->set(['email_otp'=>null])->update();

                $result = generateJWT($agentData);

                return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);

            } else {
                
                return $this->respond(['status' => 'failed','code' => 404,'data' => 'Please try again'],200);
            }

        } catch (\Exception $e) {
            return $this->respond(['status' => 'failed','code' => 500,'data' =>[], 'error' => $e->getMessage()],500);
        }
    }

  



}
