<?php
header('Access-Control-Allow-Origin: *');
header("Content-type: application/json; charset=utf-8");

//error_reporting(0);

include '../utils/database.php';
include '../utils/helper.php';

showErrors();

$dbname_process_test = "mintdoct_process_test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname_process);
// Check connection
mysqli_set_charset($conn, "utf8");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// $resultQuestions = $conn->query("SELECT * FROM questionsV2 WHERE `type`='fastfit'");
// if ($resultQuestions->num_rows > 0) {
//     while ($row_qu = $resultQuestions->fetch_assoc()) {
//         $qid = $row_qu["qid"];
//         $text = $row_qu["text"];
//         $hint = $row_qu["hint"];
//         $description = $row_qu["description"];
//         $type = $row_qu["type"];
//         $pid = $row_qu["pid"];
//         $parasiteAid = $row_qu["parasiteAid"];
//         $bgColor = $row_qu["bgColor"];
//         $textColor = $row_qu["textColor"];
//         $video = $row_qu["video"];
//         $cover = $row_qu["cover"];
//         $image = $row_qu["image"];
//         $background = $row_qu["background"];
//         $questionType = $row_qu["questionType"];
//         $inputQuestion = $row_qu["inputQuestion"];
//         $questionNumber = $row_qu["questionNumber"];
//         $hasHeader = $row_qu["hasHeader"];

//         $qidTemp = str_replace("fastfit", "", $qid);

//         // if (strpos($qidTemp, "b") !== false) {
//         //     echo $qid . "\n\n";
//         //     // $conn->query("DELETE FROM questionsV2 WHERE `qid`='$qid'");
//         // }

//         if (strpos($qidTemp, "g") !== false) {
//             $qid = "fastfit" . str_replace("g", "b", $qidTemp);
//             $parasiteAid = str_replace("g", "b", $parasiteAid);

//             $resultCountQid = $conn->query("SELECT * FROM questionsV2 WHERE `qid`='$qid'");
//             if ($resultCountQid->num_rows == 0) {

//                 echo $qid . "\n\n";

//                 $query = "INSERT INTO questionsV2 (`qid` , `text` , `hint` , `description` ,`type` , `pid` , `parasiteAid` , `bgColor` , `textColor` ,`video` , `cover` , `image` , `background` , `questionType` , `questionNumber` , `inputQuestion` , `hasHeader`) VALUES (? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?, ?)";
//                 $stmt = $conn->prepare($query);
//                 $stmt->bind_param('sssssssssssssssss', $qid, $text, $hint, $description, $type, $pid, $parasiteAid, $bgColor, $textColor, $video, $cover, $image, $background, $questionType, $questionNumber, $inputQuestion, $hasHeader);
//                 $stmt->execute();

//                 // if ($conn->query($query)) {
//                 //     echo "true" . "\n";
//                 // } else {
//                 //     echo ("Error description: " . mysqli_error($conn)) . "\n\n";
//                 // }
//             }
//         }
//     }
// }




$resultAnswers = $conn->query("SELECT * FROM answersV2 WHERE `type`='fastfit'");
if ($resultAnswers->num_rows > 0) {
    while ($row_an = $resultAnswers->fetch_assoc()) {
        $qid = $row_an["qid"];
        $text = $row_an["text"];
        $aid = $row_an["aid"];
        $description = $row_an["description"];
        $type = $row_an["type"];
        $next_question = $row_an["next_question"];
        $prev_question = $row_an["prev_question"];
        $is_result = $row_an["is_result"];
        $image = $row_an["image"];
        $priority = $row_an["priority"];

        $hintIcon = $row_an["hintIcon"];
        $hintDescription = $row_an["hintDescription"];
        $hintTitle = $row_an["hintTitle"];

        $next_questionTemp = str_replace("fastfit", "", $next_question);
        $prev_questionTemp = str_replace("fastfit", "", $prev_question);
        $aidTemp = str_replace("fastfit", "", $aid);

        $qidTemp = str_replace("fastfit", "", $qid);


        // if (strpos($next_question, "gymbymb") !== false) {
        //     $next_question_new = str_replace("gymb", "g", $next_question);
        //     $prev_question_new = str_replace("gymb", "g", $prev_question);
        //     echo $next_question_new . "\n" . $prev_question_new . "\n\n";

        //     $conn->query("UPDATE answersV2 SET `next_question`='$next_question_new' , `prev_question`='$prev_question' WHERE `aid`='$aid' AND `type`='gym_en'");
        // }

        if (strpos($qidTemp, "g") !== false) {
            $qid = "fastfit" . str_replace("g", "b", $qidTemp);
            $aid = "fastfit" . str_replace("g", "b", $aidTemp);
            $next_question = "fastfit" . str_replace("g", "b", $next_questionTemp);
            $prev_question = "fastfit" . str_replace("g", "b", $prev_questionTemp);

            $resultCountAid = $conn->query("SELECT * FROM answersV2 WHERE `aid`='$aid'");
            if ($resultCountAid->num_rows == 0) {

                echo $qid . "\n";
                echo $aid . "\n\n";

                $query = "INSERT INTO answersV2 (`qid` , `text` , `aid` , `description` ,`type` , `next_question` , `prev_question` , `is_result`, `image`, `priority` , `hintIcon` , `hintTitle` , `hintDescription`) VALUES (? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?)";
                $stmt = $conn->prepare($query);
                $stmt->bind_param('sssssssssssss', $qid, $text, $aid, $description, $type, $next_question, $prev_question, $is_result, $image, $priority, $hintIcon, $hintTitle, $hintDescription);
                $stmt->execute();

                //             $conn->query("INSERT INTO answersV2 (`qid` , `text` , `aid` , `description` ,`type` , `next_question` , `prev_question` , `is_result`, `image`, `priority` )
                //  VALUES ('$qid' , '$text' , '$aid' , '$description' , '$type' , '$next_question' , '$prev_question' , '$is_result' , '$image' , '$priority')");
            }
        }
    }
}

































// $resultParasite = $conn->query("SELECT * FROM parasite WHERE `id`>'225' AND `id`<'367'");
// if ($resultParasite->num_rows > 0) {
//     while ($row_qu = $resultParasite->fetch_assoc()) {
//         $pType = $row_qu["pType"];
//         $pid = $row_qu["pid"];
//         $imageLink = $row_qu["imageLink"];
//         $videoLink = $row_qu["videoLink"];
//         $buttonText = $row_qu["buttonText"];
//         $buttonBgColor = $row_qu["buttonBgColor"];
//         $buttonTextColor = $row_qu["buttonTextColor"];
//         $titleText = $row_qu["titleText"];
//         $descriptionText = $row_qu["descriptionText"];
//         $titleFadeTexts = $row_qu["titleFadeTexts"];
//         $transitionTexts = $row_qu["transitionTexts"];
//         $textAlign = $row_qu["textAlign"];
//         $textSize = $row_qu["textSize"];
//         $loadingType = $row_qu["loadingType"];
//         $loadingColor = $row_qu["loadingColor"];
//         $loadingTime = $row_qu["loadingTime"];
//         $loadingMaxValue = $row_qu["loadingMaxValue"];
//         $loadingText = $row_qu["loadingText"];
//         $loadingTexts = $row_qu["loadingTexts"];
//         $loadingHasButton = $row_qu["loadingHasButton"];
//         $loadingDelay = $row_qu["loadingDelay"];
//         $language = $row_qu["language"];
//         $priority = $row_qu["priority"];
//         $hasCode = $row_qu["hasCode"];

//         if (strpos($pid, "fit") !== false) {

//             $pid = str_replace("fit","inc",$pid);

//     if ($conn->query("INSERT INTO parasite(`pid` , `pType` , `priority` , `buttonBgColor` , `buttonTextColor` , `imageLink` , `buttonText` , `titleText` , `descriptionText` , `titleFadeTexts` , `transitionTexts` , textAlign , textSize , `loadingType` , `loadingColor` , `loadingTime` , `loadingHasButton` , `loadingDelay` , `hascode` ,`hascode2`) VALUES ('$pid' , '$pType' , '$priority' , '$buttonBgColor' , '$buttonTextColor' , '$imageLink' , '$buttonText' , '$titleText' , '$descriptionText' , '$titleFadeTexts' , '$transitionTexts' , '$textAlign' , '$textSize' , '$loadingType', '$loadingColor'  ,'$loadingTime' , '$loadingHasButton' ,  '$loadingDelay' ,  '$hasCode' , '$hasCode2')")) {
          
//         }
//         else {
//             echo("Error description: " . $conn -> error);
//         }

        
//     }
// }
// }

// $resultQuestions = $conn->query("SELECT * FROM questionsV2 WHERE `type`='gym'");
// if ($resultQuestions->num_rows > 0) {
//     while ($row_qu = $resultQuestions->fetch_assoc()) {
//         $qid = $row_qu["qid"];
//         $text = $row_qu["text"];
//         $hint = $row_qu["hint"];
//         $description = $row_qu["description"];
//         $type = $row_qu["type"];
//         $pid = $row_qu["pid"];
//         $parasiteAid = $row_qu["parasiteAid"];
//         $bgColor = $row_qu["bgColor"];
//         $textColor = $row_qu["textColor"];
//         $video = $row_qu["video"];
//         $cover = $row_qu["cover"];
//         $image = $row_qu["image"];
//         $background = $row_qu["background"];
//         $questionType = $row_qu["questionType"];
//         $questionNumber = $row_qu["questionNumber"];

//         $qidTemp = str_replace("gym","",$qid);

//         if (strpos($qidTemp, "g") !== false) {
//             $qid = "gym".str_replace("g", "b", $qidTemp);
//             $parasiteAid = str_replace("g", "b", $parasiteAid);

//             $resultCountQid = $conn->query("SELECT * FROM questionsV2 WHERE `qid`='$qid'");
//             if ($resultCountQid->num_rows == 0) {
//                 $conn->query("INSERT INTO questionsV2 (`qid` , `text` , `hint` , `description` ,`type` , `pid` , `parasiteAid` , `bgColor` , `textColor` ,`video` , `cover` , `image` , `background` , `questionType` , `questionNumber`) VALUES ('$qid' , '$text' , '$hint' , '$description' , '$type' , '$pid' , '$parasiteAid' , '$bgColor' , '$textColor' , '$video' , '$cover' , '$image' , '$background' , '$questionType' , '$questionNumber')");
//             }
//         }
//     }
// }

// $resultAnswers = $conn->query("SELECT * FROM answersV2 WHERE `type`='gym'");
// if ($resultAnswers->num_rows > 0) {
//    while ($row_an = $resultAnswers->fetch_assoc()) {
//        $qid = $row_an["qid"];
//        $text = $row_an["text"];
//        $aid = $row_an["aid"];
//        $description = $row_an["description"];
//        $type = $row_an["type"];
//        $next_question = $row_an["next_question"];
//        $prev_question = $row_an["prev_question"];
//        $is_result = $row_an["is_result"];
//        $image = $row_an["image"];
//        $priority = $row_an["priority"];

//        $next_questionTemp = str_replace("gym","",$next_question);
//        $prev_questionTemp = str_replace("gym","",$prev_question);
//        $aidTemp = str_replace("gym","",$aid);
//        $qidTemp = str_replace("gym","",$qid);

//        if (strpos($qidTemp, "g") !== false) {
//         $qid = "gym".str_replace("g", "b", $qidTemp);
//         $aid = "gym".str_replace("g", "b", $aidTemp);
//         $next_question = "gym".str_replace("g", "b", $next_questionTemp);
//         $prev_question = "gym".str_replace("g", "b", $prev_questionTemp);

//         $resultCountAid = $conn->query("SELECT * FROM answersV2 WHERE `aid`='$aid'");
//         if ($resultCountAid->num_rows == 0){
//             $conn->query("INSERT INTO answersV2 (`qid` , `text` , `aid` , `description` ,`type` , `next_question` , `prev_question` , `is_result`, `image`, `priority` )
//  VALUES ('$qid' , '$text' , '$aid' , '$description' , '$type' , '$next_question' , '$prev_question' , '$is_result' , '$image' , '$priority')");
//         }

//     }

//    }
// }




// $resultQuestions = $conn->query("SELECT * FROM questionsV2 WHERE `type`='inc'");
// if ($resultQuestions->num_rows > 0) {
//     while ($row_qu = $resultQuestions->fetch_assoc()) {
//         $qid = $row_qu["qid"];
//         $pid = $row_qu["pid"];

//         if (strpos($pid, "fit") !== false) {
//             $pid = str_replace("fit", "inc", $pid);

//             if ($conn->query("UPDATE questionsV2 SET `pid`='$pid' WHERE `qid`='$pid'")) {
//             }
//         }
//     }
// }