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

use Google\Cloud\Firestore\FirestoreClient;

class Reminder_model extends CI_Model
{
    // public function get_reminders($id)
    // {
    //     $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('Reminders');
    //     $documents = $citiesRef->documents();
    //     $output = array();
    //     foreach ($documents as $document) {
    //         if ($document->exists()) {
    //             $path = substr($document->path(), 8);
    //             $output[$path] = $document->data();
    //         }
    //     }
    //     return $output;
    // }

    public function get_reminder($id)
    {
        $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)
        ]);
        try {
            $document = $db->collection('Reminders')->document($id)->snapshot()->data();
            return $document;
        } catch (\Throwable $th) {
            return FALSE;
        }
    }

    public function create_reminder($reminder_id, $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)
        ]);
        $batch = $firestore->collection('Reminders')->document($reminder_id);
        $response = $batch->set($data);
        return $data;
    }



    public function update_reminder($id, $data)
    {
        $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)
        ]);
        try {
            $customerData = array();
            foreach ($data as $key => $value) {
                $d['path'] = $key;
                $d['value'] = $value;
                array_push($customerData, $d);
            }
            $batch = $db->collection('Reminders')->document($id);
            $response = $batch->update($customerData);
        } catch (\Throwable $th) {
            return FALSE;
        }
        return TRUE;
    }

    public function delete_reminder($id)
    {
        try {
            $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)
            ]);
            $firestore->collection('Reminders')->document($id)->delete();
        } catch (\Kreait\Firebase\Exception\Auth\UserNotFound $e) {
            return FALSE;
        } catch (\Kreait\Firebase\Exception\AuthException $e) {
            return FALSE;
        }
        return $id;
    }
}
