<?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_d12 = "SELECT * FROM MentorLearnerOnlineSession WHERE YEAR(SessionDate) = YEAR(CURDATE()) AND WEEK(SessionDate, 1) = WEEK(CURDATE(), 1) ORDER BY SessionDate DESC";
$result_d12 = mysqli_query($conn, $sql_d12);
$LeanerWeeklySessions = mysqli_num_rows($result_d12);

$sql_d12 = "SELECT * FROM MentorLearnerOnlineSession WHERE SessionDate >= CURDATE() AND SessionDate < CURDATE() + INTERVAL 1 DAY ORDER BY SessionDate DESC";
$result_d12 = mysqli_query($conn, $sql_d12);
$LeanerTodaySessions = mysqli_num_rows($result_d12);

$sql_d12 = " SELECT * FROM MentorLearnerOnlineSession WHERE SessionDate >= NOW() AND SessionDate < CURDATE() + INTERVAL 1 DAY ORDER BY SessionDate ASC";
$result_d12 = mysqli_query($conn, $sql_d12);
$LeanerTodayUpcomingSessions = mysqli_num_rows($result_d12);

$sql_d12 = " SELECT * FROM MentorLearnerOnlineSession WHERE SessionDate >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) AND SessionDate < NOW() ORDER BY SessionDate DESC";
$result_d12 = mysqli_query($conn, $sql_d12);
$LeanerTodayPassedSessions = mysqli_num_rows($result_d12);

$sql_d12 = "SELECT * FROM MentorLearnerOnlineSession WHERE SessionDate >= DATE_FORMAT(CURDATE(), '%Y-%m-01') AND SessionDate < DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01')";
$result_d12 = mysqli_query($conn, $sql_d12);
$LeanerMonthlySessions = mysqli_num_rows($result_d12);

$sql_d12 = "SELECT * FROM MentorLearnerOnlineSession WHERE SessionDate <= NOW() AND DATE_ADD(SessionDate, INTERVAL Duration MINUTE) > NOW()";
$result_d12 = mysqli_query($conn, $sql_d12);
$LeanerOngoingSessions = mysqli_num_rows($result_d12);

$TotalDuration = 0;
$TotalWeeklyDuration = 0;
$sql_d12 = "SELECT * FROM MentorLearnerOnlineSession    ";
$result_d12 = mysqli_query($conn, $sql_d12);
$TotalLearnerSessions = mysqli_num_rows($result_d12);
if (mysqli_num_rows($result_d12) > 0) {
    while ($row_d12 = mysqli_fetch_assoc($result_d12)) {
        $TotalDuration += (int)$row_d12['Duration'];
    }
}
$sql_d12 = "SELECT * FROM MentorLearnerOnlineSession WHERE SessionDate >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) AND SessionDate < DATE_ADD( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), INTERVAL 7 DAY ) ORDER BY SessionDate DESC  ";
$result_d12 = mysqli_query($conn, $sql_d12);
$TotalTeacherSessions = mysqli_num_rows($result_d12);
if (mysqli_num_rows($result_d12) > 0) {
    while ($row_d12 = mysqli_fetch_assoc($result_d12)) {
        $TotalWeeklyDuration += (int)$row_d12['Duration'];
    }
}

$sql_d12 = "SELECT * FROM MentorTeacherOnlineSession WHERE YEAR(SessionDate) = YEAR(CURDATE()) AND WEEK(SessionDate, 1) = WEEK(CURDATE(), 1) ORDER BY SessionDate DESC";
$result_d12 = mysqli_query($conn, $sql_d12);
$TeacherWeeklySessions = mysqli_num_rows($result_d12);

$sql_d12 = "SELECT * FROM MentorTeacherOnlineSession WHERE SessionDate >= CURDATE() AND SessionDate < CURDATE() + INTERVAL 1 DAY ORDER BY SessionDate DESC";
$result_d12 = mysqli_query($conn, $sql_d12);
$TeacherTodaySessions = mysqli_num_rows($result_d12);

$sql_d12 = " SELECT * FROM MentorTeacherOnlineSession WHERE SessionDate >= NOW() AND SessionDate < CURDATE() + INTERVAL 1 DAY ORDER BY SessionDate ASC";
$result_d12 = mysqli_query($conn, $sql_d12);
$TeacherTodayUpcomingSessions = mysqli_num_rows($result_d12);

$sql_d12 = " SELECT * FROM MentorTeacherOnlineSession WHERE SessionDate >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) AND SessionDate < NOW() ORDER BY SessionDate DESC;";
$result_d12 = mysqli_query($conn, $sql_d12);
$TeacherTodayPassedSessions = mysqli_num_rows($result_d12);

$sql_d12 = "SELECT * FROM MentorTeacherOnlineSession WHERE SessionDate >= DATE_FORMAT(CURDATE(), '%Y-%m-01') AND SessionDate < DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01')";
$result_d12 = mysqli_query($conn, $sql_d12);
$TeacherMonthlySessions = mysqli_num_rows($result_d12);

$sql_d12 = "SELECT * FROM MentorTeacherOnlineSession WHERE SessionDate <= NOW() AND DATE_ADD(SessionDate, INTERVAL Duration MINUTE) > NOW()";
$result_d12 = mysqli_query($conn, $sql_d12);
$TeacherOngoingSessions = mysqli_num_rows($result_d12);

$sql_d12 = "SELECT * FROM MentorTeacherOnlineSession    ";
$result_d12 = mysqli_query($conn, $sql_d12);
$TotalTeacherSessions = mysqli_num_rows($result_d12);
if (mysqli_num_rows($result_d12) > 0) {
    while ($row_d12 = mysqli_fetch_assoc($result_d12)) {
        $TotalDuration += (int)$row_d12['Duration'];
    }
}
$sql_d12 = "SELECT * FROM MentorTeacherOnlineSession WHERE SessionDate >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) AND SessionDate < DATE_ADD( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), INTERVAL 7 DAY ) ORDER BY SessionDate DESC  ";
$result_d12 = mysqli_query($conn, $sql_d12);
$TotalTeacherSessions = mysqli_num_rows($result_d12);
if (mysqli_num_rows($result_d12) > 0) {
    while ($row_d12 = mysqli_fetch_assoc($result_d12)) {
        $TotalWeeklyDuration += (int)$row_d12['Duration'];
    }
}


$TotalSessionsWeek = (int)$LeanerWeeklySessions + (int)$TeacherWeeklySessions;
$TotalSessionsToday = (int)$LeanerTodaySessions + (int)$TeacherTodaySessions;
$TotalSessionsTodayUpcoming = (int)$LeanerTodayUpcomingSessions + (int)$TeacherTodayUpcomingSessions;
$TotalSessionsWeekPassed = (int)$LeanerTodayPassedSessions + (int)$TeacherTodayPassedSessions;
$TotalSessionsMonth = (int)$LeanerMonthlySessions + (int)$TeacherMonthlySessions;
$TotalSessionsOngoing = (int)$LeanerOngoingSessions + (int)$TeacherOngoingSessions;
$AverageDuration =  $TotalDuration / ($TotalLearnerSessions + $TotalTeacherSessions);


$curl_post_data = array(
    'TotalSessionsWeek' => $TotalSessionsWeek,
    'TotalSessionsToday' => $TotalSessionsToday,
    'TotalSessionsTodayUpcoming' => $TotalSessionsTodayUpcoming,
    'TotalSessionsWeekPassed' => $TotalSessionsWeekPassed,
    'TotalSessionsMonth' => $TotalSessionsMonth,
    'TotalSessionsOngoing' => $TotalSessionsOngoing,
    'TotalDuration' => $TotalDuration,
    'TotalWeeklyDuration' => $TotalWeeklyDuration,
    'AverageDuration' => $AverageDuration,
);
echo json_encode($curl_post_data);
