<?php
namespace App\Models;

use CodeIgniter\Model;

class usersResetPass extends baseModel
{
   // protected $db;

    public function __construct()
    {
        parent::__construct();
    }
/*
                $res1 = [
                    'email' => $raw_array['email'],
                    'status' => 1,
                ];
 CREATE TABLE members_pending (
		id SERIAL,
		uuid UUID DEFAULT (gen_random_uuid()),
		username VARCHAR(50) NOT NULL, 
		firstname VARCHAR(50), 
		lastname VARCHAR(50), 
		email VARCHAR(50), 
		password VARCHAR(100)  NOT NULL,
		random_text VARCHAR(15),
		status INT DEFAULT 0,
		updated timestamp without time zone DEFAULT now(),
		added timestamp without time zone DEFAULT now()
	);
*/
public function resetPass($in)
{
    $change_state = $in['stage'];

    switch ($change_state) {
        case START_PASS_CHANGE:
           return $this->startPassReset($in);
            break;

        case CONFIRM_PASS_PIN:
            return $this->verifyPassReset($in);
            break;

        case CONFIRM_PASS_CHANGE:
            break;
    }

return ['error_mode'=>-1];
} 

private function verifyPassReset($in){

    $username =  isset($inx['username'])?$inx['username']:'';
    $reset_uuid =  isset($inx['reset_uuid'])?$inx['usereset_uuidrname']:''; 
    $random_text =  isset($inx['random_text'])?$inx['random_text']:'';
   // $random_text =  isset($inx['random_text'])?$inx['random_text']:'';



    return $inx = [
        'username' => '999999',
        'reset_uuid' => '999999',
        'uuid' => '00000',
        'member_uid' => '00000',
        'error_msg' => '',
        'raw_data' => $in,
    ];
}
    private function startPassReset($in)
    {
        $insert_id=0;
        $uuid='';
        $status=0;
        $passEnr = '';// $this->encryptmd5($in["password"]);
        $randomText =  rand(101010,999999);// rand(1010101,999999);
        $pdata =[];
        $username= trim($in["username"]);

        $sqlQ = "SELECT id AS member_id,uuid FROM members WHERE username='".$in["username"]."' ";
        $query = $this->db->query($sqlQ);
        $data['user_data'] = $query->getResultArray();

        if (count($data['user_data']) == 1) {
            $userData = $data['user_data'][0];
            $pdata = [
                'username'=> $in["username"],
                'member_uid'=> $userData["uuid"],
                'random_text' =>  $randomText,
                'status'=>1
            ];

            $insert_id = $this->insert_db($pdata, 'members_password_reset');
            if( $insert_id>=0){ 
                // for now 
                $sqlQ = "SELECT * FROM members_password_reset WHERE username='$username' AND random_text='$randomText' ";
                $query = $this->db->query($sqlQ);
                $new_add = $query->getResultArray()[0];
                return $inx = [
                    'insert_id' => $new_add['id'],
                    'uuid' => $new_add['uuid'],
                    'error_msg' => '',
                    'raw_data' => $in,
                ];
            }
        }

 
/*
 CREATE TABLE members_password_reset (
		id SERIAL,
		uuid UUID DEFAULT (gen_random_uuid()),
		username VARCHAR(50) NOT NULL, 
		member_uid VARCHAR(50), 
		random_text VARCHAR(15),
		status INT DEFAULT 0,
		updated timestamp without time zone DEFAULT now(),
		added timestamp without time zone DEFAULT now()
	);

        $inData =[
            'username'=> $in["username"],
            'email'=> $in["email"],
            'password'=> $passEnr,
            'firstname'=> $in["firstname"],
            'lastname'=> $in["lastname"],
            'random_text' => $randomText
        ];

        $insert_id = $this->insert_db($inData, 'members_pending');
        if( $insert_id>=0){ // for now 
            $sqlQ = "SELECT id AS insert_id,uuid FROM members WHERE username='".$in["username"]."' AND random_text='$randomText'  AND password='$passEnr' LIMIT 1";
            $query = $this->db->query($sqlQ);
            $dat = $query->getResultArray();
            $insert_id = $dat[0]['insert_id'];
            $uuid = $dat[0]['uuid'];
            $status=1;
        }
        */

        return $inx = [
            'insert_id' => '0',
            'uuid' => '',
            'error_msg' => 'Unable to continue',
            'raw_data' => $in,
        ];

    }

    public function tempCodes(){
        $sqlQ = "SELECT username,uuid,member_uid,random_text FROM members_password_reset WHERE status = 1 ORDER BY id DESC LIMIT 10";
        $query = $this->db->query($sqlQ);
       return $query->getResultArray();
    }

    private function encryptmd5($rwStstr){

        $sqlQ = "SELECT md5('$rwStstr') AS md5_string";
        log_message('critical', "encryptmd5-Entered".$sqlQ  );
        $query = $this->db->query($sqlQ);
        $data = $query->getResultArray();
        return $data[0]['md5_string'];
    }



}

/*
CREATE TABLE members_login_hx (
	id SERIAL,
	member_id INT REFERENCES members(id) NOT NULL,	
	channel VARCHAR(15), 
	added timestamp without time zone DEFAULT now(),
	loc INET
	);
ALTER TABLE ONLY members_login_hx
    ADD CONSTRAINT members_login_hx_id_key UNIQUE (id);*/
