<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
include '../../../../config/index.php';
$data = array();
$sql = "select * from SelfPacedClasses  WHERE Published='true' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $sql_cls = "select * from classes WHERE class_id='" . $row["class_id"] . "'  ";
        $result_cls = mysqli_query($conn, $sql_cls);
        if (mysqli_num_rows($result_cls) > 0) {
            while ($row_cls = mysqli_fetch_assoc($result_cls)) {
                $Courses = array();
                $sql_sfc = "select * from SelfPacedCourses WHERE SelfPacedClasses_id='" . $row["SelfPacedClasses_id"] . "'  AND Active='true' ORDER BY orderNo ASC";

                $result_sfc = mysqli_query($conn, $sql_sfc);
                if (mysqli_num_rows($result_sfc) > 0) {
                    while ($row_sfc = mysqli_fetch_assoc($result_sfc)) {
                        $sql_crs = "select * from courses WHERE course_id='" . $row_sfc["courses_id"] . "'  ";
                        $result_crs = mysqli_query($conn, $sql_crs);
                        if (mysqli_num_rows($result_crs) > 0) {
                            while ($row_crs = mysqli_fetch_assoc($result_crs)) {
                                $course_description = '';
                                if (file_exists("/home/sendusco/app/" . $row_crs["course_description"])) {
                                    $course_description = file_get_contents("/home/sendusco/app/" . $row_crs["course_description"]);
                                }
                                if (file_exists("/home/sendusco/digifunzi.com/" . $row_crs["course_description"])) {
                                    $course_description = file_get_contents("/home/sendusco/digifunzi.com//" . $row_crs["course_description"]);
                                }
                                if (file_exists("/home/sendusco/lms.digifunzi.com/" . $row_crs["course_description"])) {
                                    $course_description = file_get_contents("/home/sendusco/lms.digifunzi.com/" . $row_crs["course_description"]);
                                }

                                $IntroductionVideo = "";
                                $VideoName = "";
                                $sql_cvi = "select * from CourseIntroductionVideo WHERE SelfPacedCourses_id='" . $row_sfc["SelfPacedCourses_id"] . "'  ";
                                $result_cvi = mysqli_query($conn, $sql_cvi);
                                if (mysqli_num_rows($result_cvi) > 0) {
                                    while ($row_cvi = mysqli_fetch_assoc($result_cvi)) {
                                        $VideoName = $row_cvi["VideoName"];
                                        $IntroductionVideo = $row_cvi["Video"];
                                    }
                                }
                                $sql_20 = "select * from selfPacedLearners WHERE SelfPacedCourses_id='" . $row_sfc["SelfPacedCourses_id"] . "'    ";
                                $result_20 = mysqli_query($conn, $sql_20);
                                $NoLearners = mysqli_num_rows($result_20);

                                $sql_20 = "select * from selfPacedTeachers WHERE SelfPacedCourses_id='" . $row_sfc["SelfPacedCourses_id"] . "'    ";
                                $result_20 = mysqli_query($conn, $sql_20);
                                $NoTeachers = mysqli_num_rows($result_20);
                                $TotalSudents = (int)$NoLearners + (int)$NoTeachers;
                                $curl_post_data = array(
                                    'course_id' => $row_crs["course_id"],
                                    'course_name' => $row_crs["course_name"],
                                    'course_image' => $row_crs["course_image"],
                                    'course_description' => $course_description,
                                    'expected_outcome' => $row_crs["expected_outcome"],
                                    'delivery_mode' => $row_crs["delivery_mode"],
                                    'minimum_age' => $row_crs["minimum_age"],
                                    'required_sessions' => $row_crs["required_sessions"],
                                    'SelfPacedCourses_id' => $row_sfc["SelfPacedCourses_id"],
                                    'SelfPacedClasses_id' => $row_sfc["SelfPacedClasses_id"],
                                    'orderNo' => $row_sfc["orderNo"],
                                    'VideoName' => $VideoName,
                                    'IntroductionVideo' => $IntroductionVideo,
                                    'TotalSudents' => $TotalSudents,
                                    'Price' => $row_sfc["Price"],
                                    'Active' => $row_sfc["Active"]
                                );
                                array_push($Courses, $curl_post_data);
                            }
                        }
                    }
                }
                $description = '';
                if (file_exists("/home/sendusco/app/" . $row_cls["class_description"])) {
                    $description = file_get_contents("/home/sendusco/app/" . $row_cls["class_description"]);
                }
                if (file_exists("/home/sendusco/digifunzi.com/" . $row_cls["class_description"])) {
                    $description = file_get_contents("/home/sendusco/digifunzi.com/" . $row_cls["class_description"]);
                }
                if (file_exists("/home/sendusco/lms.digifunzi.com/" . $row_cls["class_description"])) {
                    $description = file_get_contents("/home/sendusco/lms.digifunzi.com/" . $row_cls["class_description"]);
                }

                $theme_data = array(
                    'primary' => '#feb139',
                    'secondary' => '#38aae1',
                    'accent' => '#ffd700',
                    'background' => '#fff8e1'
                );
                $gradient = "linear-gradient(90deg, #38aae1 0%, #e89b25 100%)";
                $solidColor = "#feb139";
                $buttonColor = "#feb139";
                $highlights = [
                    'Playful coding',
                    'Hands-on robotics',
                    'Boosts confidence',
                    'Sparks creativity',
                ];
                if ($row_cls["class_name"] === 'Crib') {
                    $theme_data = array(
                        'primary' => '#feb139',
                        'secondary' => '#38aae1',
                        'accent' => '#ffd700',
                        'background' => '#fff8e1'
                    );
                    $gradient = "linear-gradient(90deg, #38aae1 0%, #e89b25 100%)";
                    $solidColor = "#feb139";
                    $buttonColor = "#feb139";
                    $highlights = [
                        'Playful coding',
                        'Hands-on robotics',
                        'Boosts confidence',
                        'Sparks creativity',
                    ];
                } else if ($row_cls["class_name"] === 'Clicker') {
                    $theme_data = array(
                        'primary' => '#38aae1',
                        'secondary' => '#feb139',
                        'accent' => '#87ceeb',
                        'background' => '#f0f8ff'
                    );
                    $gradient = "linear-gradient(90deg, #25476a 0%, #38aae1 100%)";
                    $solidColor = "#38aae1";
                    $buttonColor = "#38aae1";
                    $highlights = [
                        'Fun coding projects',
                        'Intro to AI',
                        'Builds logic & focus',
                        'Real creations',
                    ];
                } else if ($row_cls["class_name"] === 'Quest') {
                    $theme_data = array(
                        'primary' => '#25476a',
                        'secondary' => '#38aae1',
                        'accent' => '#feb139',
                        'background' => '#f8fafc'
                    );
                    $gradient = "linear-gradient(90deg, #25476a 0%, #38aae1 100%)";
                    $solidColor = "#25476a";
                    $buttonColor = "#25476a";
                    $highlights = [
                        'Advanced coding & AI/ML',
                        'Real-world projects',
                        'Sharpens problem-solving',
                        'Career-ready skills',
                    ];
                }
                $curl_post_data = array(
                    'SelfPacedClasses_id' => $row["SelfPacedClasses_id"],
                    'class_id' => $row["class_id"],
                    'StarterCourse_id' => $row["StarterCourse_id"],
                    'PassMark' => $row["PassMark"],
                    'Published' => $row["Published"],
                    'class_name' => $row_cls["class_name"],
                    'class_type' => $row_cls["class_type"],
                    'class_description' => $description,
                    'age_from' => $row_cls["age_from"],
                    'age_to' => $row_cls["age_to"],
                    'Courses' => $Courses,
                    'theme' => $theme_data,
                    'description' => $description,
                    'highlights' => $highlights,
                    'gradient' => $gradient,
                    'solidColor' => $solidColor,
                    'icon' => $icon,
                    'buttonColor' => $buttonColor,
                );
                array_push($data, $curl_post_data);
            }
        }
    }
}

echo json_encode($data);
