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

class InnovativeBharatPushController extends CI_Controller
{
    public $auth_key;
    public function __construct()
    {
        parent::__construct();
        $this->load->helper("custom_validation_check");
        $this->auth_key = "asjhdgjhasgdhjgasduyasd876as76d567as5d675asd65a7s6d576v765sad765as76d576c76as5d6465";

        date_default_timezone_set("Asia/Calcutta");
    }

    public function index()
    {

        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);

        // echo "<pre>";

        $this->db->select('*');
        $this->db->where('is_send', '0');
        $this->db->limit(1, 0);
        $query  = $this->db->get('user_raw_leads');
        $result = $query->result_array();

        if (count($result) < 1) {
            echo date("Y-m-d H:i:s") .  " No data to push > " . PHP_EOL;
            exit(0);
        }
        // echo "<pre>";print_r($result);die;


        foreach ($result as $key => $row) {
            if (!isValidName($row["name"])) {
                if (isValidEmail($row["email"])) {
                    $email_arr = explode("@", $row["email"]);
                    $row["name"] = $email_arr[0];
                } else {
                    $row["name"] = "Unknown";
                    $row["email"]  = "demo@demo.com";
                }
            }

            $extra_field = "";
            $this->db->select('*');
            $this->db->where('user_id', $row["id"]);
            $query  = $this->db->get('user_loan_inquery');
            $result = $query->row_array();
            if (!empty($result)) {
                $extra_field = $result;
            }


            $row['form_location'] = "FICPAY";
            // $row['service'] = "FICPAY";
            $row['service_group'] = "FICPAY";
            $row['updated_at'] = date("Y-m-d H:i:s");
            $row['website'] = "";
            $row['message'] = "";
            $row['emp_count'] = "";
            $row['company_name'] = "";
            $row['budget'] = "";
            $row['is_responsive'] = "";
            $row['style'] = "";
            $row['additional_services'] = "";
            $row['page_name'] = "";
            $row['country'] = "";
            $row['no_of_pages'] = "";
            $row['quary'] = "";

            $dataArray = array(
                'id' => $row['id'],
                'form_location' => $row['form_location'],
                'service_group' => $row['service_group'],
                'page_name' => $row['page_name'],
                'cust_id' => $row['fip_id'],
                'name' => $row['name'],
                'email' => $row['email'],
                'phone' => $row['phone'],
                'website' => $row['website'],
                'landing_url' => $row['landing_url'],
                'state' => $row['state'],
                'message' => $row['message'],
                'emp_count' => $row['emp_count'],
                'country' => $row['country'],
                'user_ip' => $row['ip'],
                'company_name' => $row['company_name'],
                'budget' => $row['budget'],
                'service' => $row['service'],
                'no_of_pages' => $row['no_of_pages'],
                'style' => $row['style'],
                'is_responsive' => $row['is_responsive'],
                'quary' => $row['quary'],
                'created_at' => $row['act_date_time'],
                'updated_at' => $row['updated_at'],
                'is_send' => $row['is_send'],
                'additional_services' => $row['additional_services'],
                'extra_field' => $extra_field
            );

            $jsonData = json_encode($dataArray);

            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://www.innovativebharat.com/fastinfohub/MasterApi/?name=getDataFromHub",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => $jsonData, // Send data in JSON format
                CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json',  // Setting content type to JSON
                    'Authorization: ' . $this->auth_key, // Example of an authorization token
                    'X-API-Key: abcd1234',  // Example of a custom security header
                    'X-Requested-With: XMLHttpRequest',  // For identifying AJAX requests
                ),
            ));
            //echo "<pre>";print_r($response);die;
            $response = curl_exec($curl);
            curl_close($curl);
            $data = json_decode($response, true);
            // echo $response;
            // echo "<pre>"; print_r($data);die();

            $cust_id = $data['response']['service_customers'][0]['value'];

            $this->db->set('is_send', '1');
            $this->db->where('fip_id', $cust_id);
            $update = $this->db->update('user_raw_leads');
            $states = $update ? "" : "not";
            echo $cust_id . " is " . $states . " updated at " . date("Y-m-d H:i:s") . " > " . PHP_EOL;
        }
    }
}
