<?php

namespace App\Controllers;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\HTTP\IncomingRequest;

class Myfituser extends BaseController
{
    use ResponseTrait;
    protected $request;

    public function __construct()
    {
        $this->request = \Config\Services::request(); // Services::request();
    }

    public function index()
    {
    }

    public function users()
    {
        //$request = service('request');
        header('Access-Control-Allow-Origin: *');
        //header("Access-Control-Allow-Origin: http://localhost:9057 ");
        header('Access-Control-Expose-Headers: Access-Control-Allow-Origin');
        //header('Access-Control-Allow-Credentials: true ');
        //header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
        header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
        header('Content-type: application/json');

        // what is the endpoint
        $uri = urldecode(current_url(true));
        $findme = '?';
        $pos = strpos($uri, $findme);
        if ($pos > 5) {
            $uri = substr($uri, 0, $pos);
        }

        $pieces = explode('/', $uri);
        $psc = count($pieces);

        $endpoint = $psc > 0 ? $pieces[$psc - 1] : '';
        log_message('critical', "Enpoint-> ".$endpoint );

        $endpoints = [
            'account' => ['POST'],
            'login' => ['POST'],
            'reminders' => ['GET'],
            'editreminder' => ['POST'],
            'myfeed' => ['GET'],
            'calendar' => ['GET'],
            'profile' => ['GET'],
            'loginhx' => ['GET'],
            'stats' => ['GET'],
            'remcategory' => ['GET'],
            'remmode' => ['GET'],
            'resources' => ['GET'],
            'trackcategory' => ['GET'],
            'resetpass' => ['POST'],
            'tracking' => ['POST'],
            'trackinghx' => ['GET'],
            'resetpass-profile' => ['POST'],
            'home-stats' => ['GET'],
            'calendar' => ['POST']
        ];

        $res1 = [];
        if (array_key_exists($endpoint, $endpoints)) {
        } else {
            http_response_code(404);
            // tell the user product does not exist
            echo json_encode([
                'message' => 'Endpoint not found.',
                'URI' => $uri,
            ]);
        }
        //  echo "EXYTACT INPUT DATA HERE";
        $raw_json = file_get_contents('php://input');
        $raw_array = json_decode($raw_json, true);

        if ($_SERVER['REQUEST_METHOD'] == 'GET') {
            $get_param = $_GET['reqData'] ?? null;
            $raw_array = json_decode($get_param, true);
        }

        // $raw_array['fff'] = json_decode($this->request->getJSON(), true);
        log_message('critical', "Enpoint LOC2-> ".$endpoint );
        switch ($endpoint) {
            case 'login':
                //$userAccess = new \App\Models\userAccess();
                //$res1 = $userAccess->startLogin($raw_array);
                break;
            case 'account':
                //    $res = $this->dummyData($raw_array);
                break;
            case 'reminders':
                $myfitUserReminders = new \App\Models\myfitUserReminders();
                $res1 = $myfitUserReminders->readReminders($raw_array);
                break;
            case 'remcategory':
                $myfitUserReminders = new \App\Models\myfitUserReminders();
                $res1 = $myfitUserReminders->reminderCategory();
                break;
            case 'remmode':
                $myfitUserReminders = new \App\Models\myfitUserReminders();
                $res1 = $myfitUserReminders->reminderMode();
                break;
            case 'editreminder':
                $myfitUserReminders = new \App\Models\myfitUserReminders();
                $res1 = $myfitUserReminders->reminderAddEdit($raw_array);
                break;
            case 'resources':
                $myfitResources = new \App\Models\myfitResources();
                $res1 = $myfitResources->getSiteResources($raw_array);
                break;
            case 'myfeed':
                $res1 = $this->dummyData($raw_array);
                break;
            // case 'calendar':
            //     $res1 = $this->dummyData($raw_array);
            //     break;
            case 'resetpass': // reset password after login steps
                $res1 = $this->dummyData($raw_array);
                break;
            case 'profile':
                $res1 = $this->dummyData($raw_array);
                break;
            case 'stats':
                $res1 = $this->dummyData($raw_array);
                break;
            case 'loginhx':
                $myfitHx = new \App\Models\myfitHx();
                $res1 = $myfitHx->readLoginHx($raw_array);
                break;
            case 'trackcategory':
                $myfitTracking = new \App\Models\myfitTracking();
                $res1 = $myfitTracking->getTrackCategory();
                break;
            case 'tracking':
                $myfitTracking = new \App\Models\myfitTracking();
                $res1 = $myfitTracking->trackEngine($raw_array);
                break;
            case 'trackinghx':
                $myfitTracking = new \App\Models\myfitTracking();
                $res1 = $myfitTracking->readTracking($raw_array);
                break;

            case 'resetpass-profile':
               // $res1 = $this->dummyData($raw_array);
                $res1 = ( new \App\Models\myfitUserProfile() )->resetPassword($raw_array);
                break;

            case 'home-stats':
                $res1 = ( new \App\Models\userStats() )->homeStats($raw_array);
                break;

            case 'calendar':
                $res1 = (new \App\Models\userCalendar)->readCalendar($raw_array);
                break;
        }

        return $this->response->setJson($res1);
    }

    //this is dummy function to establish the endpoints before real implementations
    private function dummyData($raw_array)
    {
        return [
            'msg' => 'Not implemented yet',
            'raw_data' => $raw_array,
        ];
    }
}
