<?php
if (isset($_POST['sign'])) {
    include 'dbconnect.php';
    $username = $_POST['username'];
    $email = $_POST['email'];
    trySignUser($db, $username, $email);
}

function trySignUser($db, $username, $email)
{
    $stmt = pg_prepare($db, "check_email", 'SELECT id FROM users WHERE email = $1');
    $result = pg_execute($db, "check_email", array($email));
    $numRows = pg_num_rows($result);
    if ($numRows == 0) {
        signUser($db, $username, $email);
    } else {
        $response = array(
            "success" => false,
            "response" => "User already exists"
        );
        echo json_encode($response);
    }
}

function signUser($db, $username, $email)
{
    $phone = $_POST['phone'];
    $token = $_POST['token'];
    $password = sha1($_POST['password']);
    $profession = 'no profession';
    $description = 'no description';
     $profile = "profiles/usericon.png";
    $stmt = pg_prepare($db, "insert_user", 'INSERT INTO users(username, phone, email, password,profession,description,profile) VALUES ($1, $2, $3, $4, $5,$6, $7) RETURNING id');
    $result = pg_execute($db, "insert_user", array($username, $phone, $email, $password,$profession,$description,$profile));
    $row = pg_fetch_assoc($result);
    $userid = $row['id'];
   

    if ($userid) {
        $verify = strval(emailVerification($email));
        $response = array(
            "success" => true,
            "response" => "Step 1: Account created",
            "code" => $verify,
            "userid" => $userid
        );
        
        
        
    $stmt = pg_prepare($db, "insert_contact", 'INSERT INTO reg_contacts(phone, token, iphone,search_token,profile) VALUES ($1, $2 ,$3, $4, $5)');
    $result = pg_execute($db, "insert_contact", array($phone, $token,substr($phone,-9),$userid,$profile));
    $affectedRows = pg_affected_rows($result);
 
        
    } else {
        $response = array(
            "success" => false,
            "response" => "Error creating account"
        );
    }
    echo json_encode($response);
}

function emailVerification($email)
{
    $code = mt_rand(10000, 99999);
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://emailapi.netcorecloud.net/v5/mail/send",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => "{\"from\":{\"email\":\"info@fixafrica.co.ke\",\"name\":\"BRIDGE VERIFICATION.\"},\"subject\":\"Email Verification Code: \",\"content\":[{\"type\":\"html\",\"value\":\"VERIFICATION CODE IS:<p> $code </p> <br>Valid for the next 10 minutes.<br>\"}],\"personalizations\":[{\"to\":[{\"email\":\"$email\",\"name\":\"$email\"}]}]}",
        CURLOPT_HTTPHEADER => array(
            "api_key: f5928e237eb4142dbb609aa44feeb713",
            "content-type: application/json"
        ),
    ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        //  echo $response;
    }
    return $code;
}
?>
