<?php

define('ZAPIER', 'zapier');
require(APP_MODULES_PATH . 'zapier/libraries/REST_Controller.php');

class Lead extends REST_Controller
{

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper(ZAPIER . '/zapier');

        if (!empty($this->input->get_request_header('client-secret-key')) && !empty($this->input->get_request_header('base-url'))) {
            $client_secret_key = $this->input->get_request_header('client-secret-key');
            $client_url = $this->input->get_request_header('base-url');
        } else {
            print_r(json_encode(['status' => false, 'message' => 'client_secret_key and base_url is missing']));
            die;
        }
        if (!zapier_check_api_authentication($client_secret_key) &&  $client_url != base_url()) {
            print_r(json_encode(['status' => false, 'message' => 'client_secret_key and base_url is incorrect']));
            die;
        }

        // if (!option_exists(ZAPIER . '_verification_id') || !option_exists(ZAPIER . '_verified') || 1 != get_option(ZAPIER . '_verified') || !option_exists(ZAPIER . '_last_verification')) {
        //     print_r(['Your perfex module is deactivated']);
        //     print_r(json_encode(['status' => false, 'message' => 'module of perfex is deactivated']));
        //     die;
        // }
    }

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function index_get()
    {
        print_r(json_encode(['status' => true]));
        die;
    }

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function create_post()
    {
        $posted_data = json_decode(file_get_contents('php://input'));

        $data = [];

        if ($posted_data) {
            if(isset($posted_data->name)){
            $data["name"] = $posted_data->name;
            } else{
                print_r(json_encode(['message'=> 'Name is missing']));exit;
            }
            if(isset($posted_data->address)){
                $data['address'] = $posted_data->address;
            }
            if(isset($posted_data->title)){
                $data['title'] = $posted_data->title;
            }
            if(isset($posted_data->city)){
                $data['city'] = $posted_data->city;
            }
            if(isset($posted_data->email)){
                $data['email'] = $posted_data->email;
            }
            if(isset($posted_data->lead_value)){
                $data['lead_value'] = $posted_data->lead_value;
            }
            if(isset($posted_data->state)){
                $data['state'] = $posted_data->state;
            }
            if(isset($posted_data->website)){
                $data['website'] = $posted_data->website;
            }
            if(isset($posted_data->country)){
                $data['country'] = get_country_by_name($posted_data->country);
            }
            if(isset($posted_data->phonenumber)){
                $data['phonenumber'] = $posted_data->phonenumber;
            }
            if(isset($posted_data->zip)){
                $data['zip'] = $posted_data->zip;
            }
            if(isset($posted_data->company)){
                $data['company'] = $posted_data->company;
            }
            if(isset($posted_data->default_language)){
                $data['default_language'] = $posted_data->default_language;
            }
            if(isset($posted_data->description)){
                $data['description'] = $posted_data->description;
            }
                $data['dateadded']   = date('Y-m-d H:i:s');
           
            $data['assigned'] = get_option("zapier_lead_assigned");
            $data['source']   = get_option("zapier_lead_source");
            $data['status']   = get_option("zapier_lead_status");

                $this->db->insert(db_prefix() . 'leads', $data);
                $id = $this->db->insert_id();
                $posted_data->id = $id;
              
                print_r(json_encode($posted_data));
        } else {
            print_r(json_encode(['message'=> 'Something went wrong']));exit;
        }
    }

    public function update_post()
    {
        $posted_data = json_decode(file_get_contents('php://input'));

        $data = [];

        if ($posted_data) {
            if(isset($posted_data->name)){
            $data["name"] = $posted_data->name;
            } else{
                print_r(json_encode(['message'=> 'Name is missing']));exit;
            }
            if(isset($posted_data->address)){
                $data['address'] = $posted_data->address;
            }
            if(isset($posted_data->title)){
                $data['title'] = $posted_data->title;
            }
            if(isset($posted_data->city)){
                $data['city'] = $posted_data->city;
            }
            if(isset($posted_data->email)){
                $data['email'] = $posted_data->email;
            } else{
                print_r(json_encode(['message'=> 'Email is missing']));exit;
            }
            if(isset($posted_data->lead_value)){
                $data['lead_value'] = $posted_data->lead_value;
            }
            if(isset($posted_data->state)){
                $data['state'] = $posted_data->state;
            }
            if(isset($posted_data->website)){
                $data['website'] = $posted_data->website;
            }
            if(isset($posted_data->country)){
                $data['country'] = get_country_by_name($posted_data->country);
            }
            if(isset($posted_data->phonenumber)){
                $data['phonenumber'] = $posted_data->phonenumber;
            }
            if(isset($posted_data->zip)){
                $data['zip'] = $posted_data->zip;
            }
            if(isset($posted_data->company)){
                $data['company'] = $posted_data->company;
            }
            if(isset($posted_data->default_language)){
                $data['default_language'] = $posted_data->default_language;
            }
            if(isset($posted_data->description)){
                $data['description'] = $posted_data->description;
            }
           
            $data['assigned']   = get_option("zapier_lead_assigned");
            $data['source']     = get_option("zapier_lead_source");
            $data['status']     = get_option("zapier_lead_status");

             //Update customer
                
            $this->db->where('email', $posted_data->email);
            $this->db->limit(1);
     
            $id = $this->db->get('leads')->row('id');
            
            $this->load->model('leads_model');
            $id = $this->leads_model->update($data, $id);
            
                $posted_data->id = $id;
              
                print_r(json_encode($posted_data));
        } else {
            print_r(json_encode(['message'=> 'Something went wrong']));exit;
        }
    }

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function createHook_post($hooktype)
    {
        zapier_add_hook($hooktype, $_GET['url']);
        print_r(json_encode(['status' => true, 'message' => 'subscribed successfully']));
        die;
    }

    /**
     * Get All Data from this method
     *
     * @return Response
     */
    public function removeHook_delete()
    {
        zapier_delete_hook($_GET['url']);
        print_r(json_encode(['status' => true, 'message' => 'unsubscribed successfully']));
        die;
    }
}
