<?php

function manageSchedulerTask($conn_rjllearning, $workspace_id, $institution_id, $type, $type_id, $date, $time, $status_to_change, $process_type
// , $task_id = null
)
{

    try {
        // process type can either be create, alter or delete
        if ($process_type == 'create') {
            // insert
            $sql = "INSERT into scheduler_tasks(workspace_id, institution_id, type, type_id, date, time, status_to_change) values (?, ?, ?, ?, ?, ?, ?)";
            $insert = $conn_rjllearning->prepare($sql);
            $insert->execute([$workspace_id, $institution_id, $type, $type_id, $date, $time, $status_to_change]);
        } else if ($process_type == 'alter') {
            // update using workspace_id and institution_id and type and type_id 
            $sql = "UPDATE  scheduler_tasks set date = ?, time = ? where workspace_id = ? and institution_id = ? and type = ? and type_id = ?";
            $update = $conn_rjllearning->prepare($sql);
            $update->execute([$date, $time, $workspace_id, $institution_id, $type, $type_id]);
        } else if ($process_type == 'delete') {
            $sql = "DELETE from scheduler_tasks where workspace_id = ? and institution_id = ? and type = ? and type_id = ?";
            $delete = $conn_rjllearning->prepare($sql);
            $delete->execute([$workspace_id, $institution_id, $type, $type_id]);
        }
        return true;
    } catch (PDOException $e) {
        return "Error: " . $e->getMessage();
    }
}

// below function is used to publish or block tasks in specific dbs depeneding on publish and block cron job
function publishBlockTasks($conn_rjllearning, $workspace_id, $institution_id, $type, $type_id, $status_to_change, $scheduler_tasks_id)
{

    try {
        // Fetch database name based on workspace ID and status
        $sql = "SELECT db_name FROM workspace_database_info WHERE workspace_id = ? AND status = ?";
        $fetch_workspace = $conn_rjllearning->prepare($sql);
        $fetch_workspace->execute([$workspace_id, 1]);
        $result_workspace = $fetch_workspace->fetchAll();

        if (count($result_workspace) < 1) {
            errorreturn('Fatal error (1). Access denied.');
        }

        $db_name = $result_workspace[0]['db_name'];
        $conn_rjllearning_specific = dynamicDbConnection($db_name);

        //then according type go to the table and perform the assignment_stauts = status_to_change
        if ($status_to_change == '0' or $status_to_change == '1') {
            if ($type == 'assignment') {
                $sql = "UPDATE assignments set status =  ?  where institution_id = ? and id = ?";
                $update = $conn_rjllearning_specific->prepare($sql);
                $update->execute([$status_to_change, $institution_id, $type_id]);
            } elseif ($type == 'lesson') {
                $sql = "UPDATE lessons set status =  ?  where institution_id = ? and id = ?";
                $update = $conn_rjllearning_specific->prepare($sql);
                $update->execute([$status_to_change, $institution_id, $type_id]);
            } elseif ($type == 'activity') {


                $sql = "UPDATE activity_mentor set status =  ?  where institution_id = ? and id = ?";
                $update = $conn_rjllearning_specific->prepare($sql);
                $update->execute([$status_to_change, $institution_id, $type_id]);
            } elseif ($type == 'book_issue') {


                $sql = "UPDATE book_issues set status =  ?  where institution_id = ? and  id = ?";
                $update = $conn_rjllearning_specific->prepare($sql);
                $update->execute([$status_to_change, $institution_id, $type_id]);
            }
        }
        // Update the status of the scheduler task
       
    } catch (Exception $e) {
        errorreturn($e->getMessage());
    }
}

                                   
function updateSchedulerTaskStatus($conn_rjllearning, $workspace_id, $institution_id, $type, $scheduler_tasks_id, $status_to_change) {
    try {
        $sql = "UPDATE scheduler_tasks SET status = ?, status_to_change = ? WHERE workspace_id = ? AND institution_id = ? AND type = ? AND id = ?";
        $update = $conn_rjllearning->prepare($sql);
        $update->execute([1, $status_to_change, $workspace_id, $institution_id, $type, $scheduler_tasks_id]);
    } catch (Exception $e) {
        errorreturn($e->getMessage());
    }
}


function getStudentInfoForCronJob($conn_rjllearning_specific, $institution_id, $student_id)
{
 
    $res = $conn_rjllearning_specific->prepare("SELECT id FROM students WHERE user_id = ? and institution_id=? ");
    $res->execute(array($student_id, $institution_id));
    $result_faculty = $res->fetchAll();
    if(count($result_faculty) > 0) {
        $student_info =  array("student_id" => $result_faculty[0]['id']);
    } else {
        errorreturn("student not found.");
    }
    return $student_info;
}

