<?php

namespace App\Controllers;

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

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

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

    public function index()
    {
    }

    public function users()
    {
        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 = current_url(true);
        $pieces = explode('/', $uri);
        $psc = count($pieces);

        $endpoint = $psc > 0 ? $pieces[$psc - 1] : '';

        $endpoints = [
            'account' => ['POST'],
            'login' => ['POST'],
            'resetpass' => ['POST'],
            'signup-code' => ['POST'],
            'resetpass-code'  => ['POST'],
            'test-email'  => ['POST'],
        ];

        // foreach (getallheaders() as $name => $value) {
        //     log_message('critical', "HEADER $name: $value" );
        // }

        $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.']);
        }
        //  echo "EXYTACT INPUT DATA HERE";
        $raw_json = file_get_contents('php://input');
        $raw_array = json_decode($raw_json, true);

        switch ($endpoint) {
            case 'login':
                $userAccess = new \App\Models\userAccess();
                $res1 = $userAccess->startLogin($raw_array);
                break;
            case 'account':
                $userSignUp =  new \App\Models\userSignUp();
                //$this->push();
                $res1 = $userSignUp->startSignUp($raw_array);
                break;

            case 'resetpass':
                $passReset =  new \App\Models\usersResetPass();
                $res1 = $passReset->resetPass($raw_array);
            break;

            case 'signup-code':
                $userSignUp =  new \App\Models\userSignUp();
                $res1 = $userSignUp->tempCodes();
                break;

            case 'resetpass-code':
                $passReset =  new \App\Models\usersResetPass();
                $res1 = $passReset->tempCodes();
                break;

                case 'test-email':
                  $res1 =  $this->push();
                    break;

        }

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

    public function push()
    {
        $data =[];
        $to = 'ameye@chiefsoft.com';//Type here the mail address where you want to send
        $subject = 'myFit Signup';//Write here Subject of Email
        $message='Conngrats ! You did it. -- '.rand(1000,9999);//Write the message you want to send
        $email =  \Config\Services::email();

    $config['protocol'] = 'sendmail';
    $config['mailPath'] = '/usr/sbin/sendmail';
    $config['charset']  = 'iso-8859-1';
    $config['wordWrap'] = true;

    //$email->initialize($config);

        $email->setTo($to);
        $email->setFrom('support@chiefsoft.com', 'Just testing the emsil function');//set From
        $email->setSubject($subject);
        $email->setMessage($message);
        if($email->send())
        {
        //echo 'Email has been Sent.';
        log_message('critical', "Email has been Sent" );
        $data['msg'] ='All good '.rand(100,9999);
        }
        else{
       // echo 'Something went wrong !';
       $data['error'] = $email->printDebugger(['headers']);

        log_message('critical', "Something went wrong" );
        //log_message('critical',$data );
        
        //print_r($data);
        }
        return $data;
    }
    //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,
        ];
    }
}
