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

use Google\Cloud\Firestore\FirestoreClient;

class Common extends CI_Model
{

    function adddatamodel($collectionname, $documentname, $data)
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $cityRef = $firestore->collection($collectionname)->document($documentname);

        if ($cityRef->set($data)) {
            return true;
        } else {
            return false;
        }
    }
    function adddatamodelnexted($collectionname, $documentname, $collectionname1, $documentname1, $data)
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $cityRef = $firestore->collection($collectionname)->document($documentname)->collection($collectionname1)->document($documentname1);

        if ($cityRef->set($data)) {
            return true;
        } else {
            return false;
        }
    }

    function getAddress($latitude, $longitude)
    {
        //google map api url
        $API_KEY = "AIzaSyAe_Fm2ja1OuDHswu4mgZKQN9DRakGyzzs";
        $url = "https://maps.google.com/maps/api/geocode/json?latlng=$latitude,$longitude&key=" . $API_KEY;
        // send http request
        $geocode = file_get_contents($url);
        $json = json_decode($geocode);
        $address = $json->results[0]->formatted_address;
        return $address;
    }
    function readClientChatImages($documentname = "", $documentname1 = "")
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $collectionName = 'ClientChats';

        $docRef = $firestore->collection($collectionName)->document($documentname)->collection('Conversations')->document($documentname1);
        $snapshot = $docRef->snapshot();

        $images = array();

        if ($snapshot->exists()) {
            $j = $snapshot->data();
            for ($i = 0; $i == $i; $i++) {
                if ($j['image' . $i]) {
                    $images['image' . $i] = $j['image' . $i];
                } else {
                    break;
                }
            }
        }

        return $images;
    }

    function get_iechartdata()
    {
        $date = $this->createDateRangeArray(date('Y-m-d', strtotime('-5 day')), date('Y-m-d'));
        $arr  = array();
        foreach ($date as $key => $dates) {
            $income = 0;
            $expense = 0;
            $triplist = $this->readdatafromcollectionbetween("Bookings", "bookingDate", ">", strtotime($dates) * 1000, "bookingDate", "<", strtotime($dates) * 86400000);
            if (!empty($triplist)) {
                foreach ($triplist as $tripId => $tripDetails) {
                    $income = $income + ((float)$tripDetails["price"] * 20 / 100);
                }
            }
            $triplist = $this->readdatafromcollectionbetween("Bookings", "bookingDate", ">", strtotime($dates) * 1000, "bookingDate", "<", strtotime($dates) * 86400000);
            if (!empty($triplist)) {
                foreach ($triplist as $tripId => $tripDetails) {
                    $expense = $expense + ((float)$tripDetails["price"] * 80 / 100);
                }
            }
            // $triplist = $this->readdatafromcollectionbetweenwhere("Payouts", "status" , "=", "paid", "completionTimeStamp" , ">", strtotime($dates)*1000, "completionTimeStamp" , "<", strtotime($dates)*86400000);
            // if(!empty($triplist)) {
            //     foreach ($triplist as $tripId => $tripDetails) {
            //         $expense = $expense + (float)$tripDetails["amount"];
            //     }
            // }
            $arr[$dates]['income']  = ($income) ? $income : 0;
            $arr[$dates]['expense'] = ($expense) ? $expense : 0;
        }
        return $arr;
    }

    function createDateRangeArray($strDateFrom, $strDateTo)
    {
        $aryRange  = array();
        $iDateFrom = mktime(1, 0, 0, substr($strDateFrom, 5, 2), substr($strDateFrom, 8, 2), substr($strDateFrom, 0, 4));
        $iDateTo   = mktime(1, 0, 0, substr($strDateTo, 5, 2), substr($strDateTo, 8, 2), substr($strDateTo, 0, 4));
        if ($iDateTo >= $iDateFrom) {
            array_push($aryRange, date('Y-m-d', $iDateFrom)); // first entry
            while ($iDateFrom < $iDateTo) {
                $iDateFrom += 86400; // add 24 hours
                array_push($aryRange, date('Y-m-d', $iDateFrom));
            }
        }
        return $aryRange;
    }


    function updatedatamodel($collectionname = "", $documentname = "", $data = array())
    {

        $ceta = array();
        foreach ($data as $key => $value) {
            $d['path'] = $key;
            $d['value'] = $value;
            array_push($ceta, $d);
        }

        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $cityRef = $firestore->collection($collectionname)->document($documentname);

        $cityRef->update($ceta);
        return true;
    }

    function deletedatamodel($collectionname, $documentname)
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        if ($firestore->collection($collectionname)->document($documentname)->delete()) {
            return true;
        } else {
            return false;
        }
    }
    function nexteddeletedatamodel($collectionname, $documentname, $collectionname1, $documentname1)
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        if ($firestore->collection($collectionname)->document($documentname)->collection($collectionname1)->document($documentname1)->delete()) {
            return true;
        } else {
            return false;
        }
    }

    //getcounternotification
    function getcounternotification()
    {
        $citiesRef = "";
        $output = array();
        $baby = 0;
        $documents = "";
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $db = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $citiesRef = $db->collection('AssignNoty');
        $documents = $citiesRef->documents();

        foreach ($documents as $document) {
            if ($document->exists()) {
                $da = $document->data();

                if ($da['isRead'] == false) {
                    $baby = $baby + 1;
                }
            }
        }

        $citiesRef = $db->collection('AdminChats');
        $documents = $citiesRef->documents();

        foreach ($documents as $document) {
            if ($document->exists()) {
                $da = $document->data();

                if ($da['lastMessage']['isRead'] == false) {
                    $baby = $baby + 1;
                }
            }
        }

        return $baby;
    }

    function getnotifications()
    {
        $citiesRef = "";
        $output = array();
        $documents = "";
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $db = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $citiesRef = $db->collection('AssignNoty');
        $documents = $citiesRef->documents();
        $output = array();
        foreach ($documents as $document) {
            if ($document->exists()) {
                $da = $document->data();
                if ($da['isRead'] == false) {
                    $output[$document->id()] = $document->data();
                }
            }
        }
        return $output;
    }

    function getunreadchats()
    {
        $citiesRef = "";
        $output = array();
        $documents = "";
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $db = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $citiesRef = $db->collection('AdminChats');
        $documents = $citiesRef->documents();
        $output = array();
        foreach ($documents as $document) {
            if ($document->exists()) {
                $da = $document->data();
                if ($da['lastMessage']['isRead'] == false) {
                    $output[$document->id()] = $da['lastMessage'];
                }
            }
        }
        return $output;
    }



    function readusername($documentname = "")
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $docRef = $firestore->collection("Users")->document($documentname);
        $snapshot = $docRef->snapshot();

        if ($snapshot->exists()) {
            $j = $snapshot->data();
           
                return $j['fullname'] ;
        } else {
            return '';
        }
    }

    function isclient($documentname = "")
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $docRef = $firestore->collection("Users")->document($documentname);
        $snapshot = $docRef->snapshot();
        //        var_dump($snapshot);

        if ($snapshot->exists()) {
            $j = $snapshot->data();
            if ($j['userRole'] == "client")
                return TRUE;
            else
                return FALSE;
        } else {
            return FALSE;
        }
    }
    function readclientname($documentname = "")
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $docRef = $firestore->collection("Users")->document($documentname);
        $snapshot = $docRef->snapshot();
        //        var_dump($snapshot);

        if ($snapshot->exists()) {
            $j = $snapshot->data();
            if (!empty($j['facilityName']) && $j['facilityName'] != "")
                return $j['facilityName'];
            else
                return $j['firstName'] . " " . $j['lastName'];
        } else {
            return '';
        }
    }
    function readattributename($collection, $attribute, $documentname = "")
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $docRef = $firestore->collection($collection)->document($documentname);
        $snapshot = $docRef->snapshot();
        //        var_dump($snapshot);

        if ($snapshot->exists()) {
            $j = $snapshot->data();

            return $j[$attribute];
        } else {
            return '';
        }
    }

    function readtravelname($documentname = "")
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $docRef = $firestore->collection("Travels")->document($documentname);
        $snapshot = $docRef->snapshot();

        if ($snapshot->exists()) {
            $j = $snapshot->data();
            if (!empty($j['name']) && $j['name'] != "")
                return $j['name'];
            else
                return " ";
        } else {
            return '';
        }
    }

    function readImage($documentname = "")
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $docRef = $firestore->collection("Users")->document($documentname);
        $snapshot = $docRef->snapshot();

        if ($snapshot->exists()) {
            $j = $snapshot->data();
            return $j['image'];
        } else {
            return '';
        }
    }

    function readclientImage($documentname = "")
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $docRef = $firestore->collection("clients")->document($documentname);


        $snapshot = $docRef->snapshot();

        if ($snapshot->exists()) {
            $j = $snapshot->data();
            return $j['image'];
        } else {
            return '';
        }
    }


    function readdatadocument2($collectionname = "", $documentname = "",$collectionname1 = "", $documentname1 = "")
    {

        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $docRef = $firestore->collection($collectionname)->document($documentname)->collection($collectionname1)->document($documentname1);
        $snapshot = $docRef->snapshot();

        if ($snapshot->exists()) {

            return $snapshot->data();
        } else {
            return '';
        }
    }

    function readdatadocument($collectionname = "", $documentname = "")
    {

        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $docRef = $firestore->collection($collectionname)->document($documentname);
        $snapshot = $docRef->snapshot();

        if ($snapshot->exists()) {

            return $snapshot->data();
        } else {
            return '';
        }
    }

    function search($q="a")
    {
 $searchTerm =$q;
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $citiesRef = $firestore->collection("Users")
                  ->where('firstName', '>=', $searchTerm)
                  ->where('firstName', '<=', $searchTerm . "\uf8ff")
                  ->orderBy('firstName')
                  ->limit(10); ;
        $snapshot1 = $citiesRef->documents();

        $query2 = $firestore->collection("Users")->where('lastName', '>=', $searchTerm)
                  ->where('lastName', '<=', $searchTerm . "\uf8ff")
                  ->orderBy('lastName')
                  ->limit(10); // Limit the number of results
        $snapshot2 = $query2->documents();         

        $results = [];

// Combine results from both queries
foreach ($snapshot1 as $document) {
    $results[$document->id()] = $document->data();
}
foreach ($snapshot2 as $document) {
    $results[$document->id()] = $document->data();
}

// Convert results to indexed array
$results = array_values($results);
//print_r($results);
return $results;

    }

    function readdatacollection($collectionname, $subCollectionname, $documentname)
    {
        $citiesRef = "";
        $documents = "";
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $db = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        if ($subCollectionname != "") {
            $citiesRef = $db->collection($collectionname)->document($documentname)->collection($subCollectionname);
            $documents = $citiesRef->documents();
        } else {
            $citiesRef = $db->collection($collectionname);
            $documents = $citiesRef->documents();
        }

        $output = array();

        foreach ($documents as $document) {
            if ($document->exists()) {
                $j = array();
                $output[$document->id()] = $document->data();
            }
        }

        return $output;
    }

    function nextedreaddatafromcollectionwhereclause($collectionname, $doc, $collection1)
    {
        $citiesRef = "";
        $documents = "";
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $db = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $citiesRef = $db->collection($collectionname)->document($doc)->collection($collection1);

        $documents = $citiesRef->documents();

        $output = array();

        foreach ($documents as $document) {
            if ($document->exists()) {
                $j = array();
                $output[$document->id()] = $document->data();
            }
        }

        return $output;
    }

    function readdatafromcollectionwhereclause($collectionname, $collectionkey = "", $collectiontype = "", $collectionkeyvalue = "")
    {
        $citiesRef = "";
        $documents = "";
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $db = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        if ($collectionkey != "" && $collectiontype != "" && $collectionkeyvalue != "") {
            $citiesRef = $db->collection($collectionname)->where($collectionkey, $collectiontype, $collectionkeyvalue);
            $documents = $citiesRef->documents();
        } else {
            $citiesRef = $db->collection($collectionname);
            $documents = $citiesRef->documents();
        }

        $output = array();

        foreach ($documents as $document) {
            if ($document->exists()) {
                $j = array();
                $output[$document->id()] = $document->data();
            }
        }

        return $output;
    }

    function readdatafromcollectionbetween($collectionname, $collectionkey = "", $collectiontype = "", $collectionkeyvalue = "", $collectionkey2 = "", $collectiontype2 = "", $collectionkeyvalue2 = "")
    {
        $citiesRef = "";
        $documents = "";
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');

        $db = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);
        if ($collectionkey2 != "" && $collectiontype2 != "" && $collectionkeyvalue2 != "") {
            $citiesRef = $db->collection($collectionname)->where($collectionkey, $collectiontype, $collectionkeyvalue)->where($collectionkey2, $collectiontype2, $collectionkeyvalue2);
            $documents = $citiesRef->documents();
        } elseif ($collectionkey != "" && $collectiontype != "" && $collectionkeyvalue != "") {
            $citiesRef = $db->collection($collectionname)->where($collectionkey, $collectiontype, $collectionkeyvalue);
            $documents = $citiesRef->documents();
        } else {
            $citiesRef = $db->collection($collectionname);
            $documents = $citiesRef->documents();
        }

        $output = array();

        foreach ($documents as $document) {
            if ($document->exists()) {
                $j = array();
                $output[$document->id()] = $document->data();
            }
        }

        return $output;
    }

    function readnumberofdocumentsincollectionbetween($collectionname, $collectionkey = "", $collectiontype = "", $collectionkeyvalue = "", $collectionkey2 = "", $collectiontype2 = "", $collectionkeyvalue2 = "")
    {
        $citiesRef = "";
        $documents = "";
        $documentsCount = 0;
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');

        $db = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);
        if ($collectionkey2 != "" && $collectiontype2 != "" && $collectionkeyvalue2 != "") {
            $citiesRef = $db->collection($collectionname)->where($collectionkey, $collectiontype, $collectionkeyvalue)->where($collectionkey2, $collectiontype2, $collectionkeyvalue2);
            $documents = $citiesRef->documents();
        } elseif ($collectionkey != "" && $collectiontype != "" && $collectionkeyvalue != "") {
            $citiesRef = $db->collection($collectionname)->where($collectionkey, $collectiontype, $collectionkeyvalue);
            $documents = $citiesRef->documents();
        } else {
            $citiesRef = $db->collection($collectionname);
            $documents = $citiesRef->documents();
        }

        foreach ($documents as $document) {
            if ($document->exists()) {
                $documentsCount++;
            }
        }

        return $documentsCount;
    }

    function readdatafromcollectionbetweenwhere($collectionname, $collectionkey = "", $collectiontype = "", $collectionkeyvalue = "", $collectionkey2 = "", $collectiontype2 = "", $collectionkeyvalue2 = "", $collectionkey3 = "", $collectiontype3 = "", $collectionkeyvalue3 = "")
    {
        $citiesRef = "";
        $documents = "";
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');

        $db = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);
        if ($collectionkey3 != "" && $collectiontype3 != "" && $collectionkeyvalue3 != "") {
            $citiesRef = $db->collection($collectionname)->where($collectionkey, $collectiontype, $collectionkeyvalue)->where($collectionkey2, $collectiontype2, $collectionkeyvalue2)->where($collectionkey3, $collectiontype3, $collectionkeyvalue3);
            $documents = $citiesRef->documents();
        } else {
            $citiesRef = $db->collection($collectionname)->where($collectionkey, $collectiontype, $collectionkeyvalue);
            $documents = $citiesRef->documents();
        }

        $output = array();

        foreach ($documents as $document) {
            if ($document->exists()) {
                $j = array();
                $output[$document->id()] = $document->data();
            }
        }

        return $output;
    }


    function readdatadocumentwithcondition($collectionname = "Settings", $documentname = "testing", $where = "")
    {
        $x = $this->config->item('firebase_app_key');
        $firebaseProjectID = $this->config->item('firesbase_project_id');
        $firestore = new FirestoreClient([
            'projectId' => $firebaseProjectID,
            'keyFile' => json_decode(file_get_contents($x), true)
        ]);

        $docRef = $firestore->collection($collectionname)->document($documentname);
        $snapshot = $docRef->snapshot();

        if ($snapshot->exists()) {

            return $snapshot->data();
        } else {
            return '';
        }
    }
}
