<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Api_auth extends CI_Controller {

    public function __construct(){
        
		parent::__construct();
        
    }
    
    
    

    public function index()
    {
           
        $sql =  "SELECT * FROM tbl_api_auth";
        $api_auth= $this->db->query($sql)->row_array();
		
        if(!empty($api_auth))
        {
           $response['username']=$api_auth['username'];
           $response['key']=$api_auth['password'];
           
           $status='success';
           $statusCode=200;
           $message='Data found';
        }
        else
        {
           $status='error';
           $statusCode=400;
           $message='You are not authorized to access application. please contact to support.';
        }
        
        $response['status']=$status;
        $response['statusCode']=$statusCode;
		$response['message']=$message;
		
		$dataToEncrypt = json_encode($response);;
        $encryptedData = bin2hex($dataToEncrypt);
        
        echo '"'.$encryptedData.'"';
	    
    }
    
    
    
            public function fetch_api_auth(){
 
        header("Access-Control-Allow-Origin: *");
		header("Content-Type: application/json; charset=UTF-8");
		$status='error';
		$message='Invalid Request';
        $statusCode=400;
 
        $json = $this->security->xss_clean(file_get_contents('php://input'));
        $data = json_decode($json);
 
 
                $api_auth=$this->db->query("SELECT * FROM  tbl_api_auth ")->result_array();
              
 
 
                if(!empty($api_auth))
                {
 
                $i=0;
                foreach($api_auth as $row)
                {
                    $response['api_auth'][$i]['id']=$row['id'];
                    $response['api_auth'][$i]['username']=$row['username'];
                    $response['api_auth'][$i]['password']=$row['password'];
 
 
                    $i++;
                } 
            
 
              
 
                    $response['api_auth']=$response['api_auth'];
                    $message='Data found';
                    $status='success';
                    $statusCode=200;
                }
                else
                {
                    $status='error';
                    $message='Data not found';
                    $statusCode=400;
                }
 
 
 
 
        $response['status']=$status;
        $response['statusCode']=$statusCode;
		$response['message']=$message;
		echo json_encode($response);
    }
    
        
  public function support_info()
{
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json");

    $json = file_get_contents('php://input');
    $data = json_decode($json);

    // Check if user_type is provided
    if (!isset($data->user_type) || empty($data->user_type)) {
        echo json_encode([
            'status' => 'error',
            'message' => 'User type is required'
        ]);
        return;
    }

    $user_type = $this->security->xss_clean($data->user_type);

    // Allowed user types
    $allowed_types = ['user', 'driver'];

    // Validate value
    if (!in_array(strtolower($user_type), $allowed_types)) {
        echo json_encode([
            'status' => 'error',
            'message' => 'Invalid user type. Allowed values: user, seller, deliveryboy'
        ]);
        return;
    }

    // Fetch from DB
    $info = $this->db->get_where('support_info', ['type' => $user_type])->row();

    if ($info) {
        echo json_encode([
            'status' => 'success',
            'data' => $info
        ]);
    } else {
        echo json_encode([
            'status' => 'error',
            'message' => 'No data found for this user type'
        ]);
    }
}
    
            public function App_version(){
 
        header("Access-Control-Allow-Origin: *");
		header("Content-Type: application/json; charset=UTF-8");
		$status='error';
		$message='Invalid Request';
        $statusCode=400;
 
        $json = $this->security->xss_clean(file_get_contents('php://input'));
        $data = json_decode($json);
 
         $applicationid=$data->applicationid;
         
          if(isset($applicationid))
        {
            if($applicationid )
            {
                $App_version=$this->db->query("SELECT * FROM  app_versionstatus where applicationid='$applicationid'")->row_array();
              
 
 
                if(!empty($App_version))
                {
                        
                        if($App_version['isAppUpdateMendatory']==1)
                        {
                             $response['versionname']=$App_version['versionname'];
                            $response['versioncode']=$App_version['versioncode'];
                            $response['isAppUpdateMendatory']=$App_version['isAppUpdateMendatory'];
                            $update_message="New update is available on play store please update the App.";
                            $message='Data found';
                            $status='success';
                            $statusCode=200;
                         }
                        else
                        {
                           
                             $response['versionname']=$App_version['versionname'];
                            $response['versioncode']=$App_version['versioncode'];
                            $response['isAppUpdateMendatory']=$App_version['isAppUpdateMendatory'];
                            $update_message="version is same updation not available";
                            $message='Data found';
                            $status='success';
                            $statusCode=200;
                        }
 
                 
                   
                }
                else
                {
                   
                    $status='error';
                    $message='Data not found';
                    $statusCode=400;
                }
 
            }
            else
            {
                $status='error';
                $message='Please fill all required fields';
                $statusCode=400;
            }
        }
        else
        {
            $status='error';
            $message='Please fill all required fields';
            $statusCode=400;
        }
        
 
 
        $response['update_message']=$update_message;
        $response['status']=$status;
        $response['statusCode']=$statusCode;
		$response['message']=$message;
		echo json_encode($response);
    }
}