<?php

session_start();

header('Access-Control-Allow-Origin:*');

header('Content-Type; application/json; charset=utf-8');

header('Access-control-Allow-Method: POST');

header('Access-control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-request-With');

include('db.php');


    $inputdata = json_decode(file_get_contents("php://input"), true);

    $nom = $inputdata['nom'];

    $pre = $inputdata['pre'];

    $email = $inputdata['email'];

    $password = $inputdata['password'];

    $passwordH = password_hash($password, 1);

    $code_perso = substr($nom,0,1).''.substr($pre,0,1).'_'.random_int(100, 9999);

    $code_parrain = $inputdata['code_parrain'];

    $otp = random_int(1000, 9999);

    $nbr="";

    
    // verifier si mail existant

    $email_verif=$db->prepare('SELECT COUNT(id) AS nbr FROM user WHERE email=?');

    $email_verif->execute(array($email));

    while($email_verifs=$email_verif->fetch()){

        $nbr=$email_verifs['nbr'];

    }

     if ($nbr == 0) {

        if (empty($code_parrain)) {

                # inscription sans code parrain...

                    $user=$db->prepare('INSERT INTO user (nom, pre, email, password, code_perso, otp) VALUES(?,?,?,?,?,?)');

                    $req_user = $user->execute(array($nom, $pre, $email, $passwordH, $code_perso, $otp));

                    if ($req_user) {

                            # tout s'est bien passé...
                        
                            include('mail_inscription.php');

                            echo (json_encode(['response' => 1]));



                    }else{

                        echo (json_encode(['response' => 0]));

                        //message 0 : erreur inscription

                    }

        }else {

            $valid_cod_parrain="";

            $pseudo_verif=$db->prepare('SELECT COUNT(id) AS nbr FROM user WHERE code_perso=?');

            $pseudo_verif->execute(array($code_parrain));

            while($pseudo_verifs=$pseudo_verif->fetch()){

                $valid_cod_parrain=$pseudo_verifs['nbr'];

            }

            if ($valid_cod_parrain == 1) {


                        $user=$db->prepare('INSERT INTO user (nom, pre, email, password, code_perso, code_parrain, otp) VALUES(?,?,?,?,?,?,?)');

                        $req_user = $user->execute(array($nom, $pre, $email, $passwordH, $code_perso, $code_parrain, $otp));

                        if ($req_user) {

                                # tout s'est bien passé...
                                include('mail_inscription.php');

                                echo (json_encode(['response' => 1]));



                        }else{

                            echo (json_encode(['response' => 0]));

                            //message 0 : erreur inscription

                        }       
      

            }else {

                echo (json_encode(['response' => -1]));

                //error 2 : ce code parrain est invalide

            }  

        }


     }else {

        echo (json_encode(['response' => -2]));

        //error 1 : cet email existe dejà

     }



//}



