<?php
ini_set('display_errors', 1);

require_once '../../../../resources/base_path_include.php';

require_once $base_path_to_learning_resources  . "includes/connect.php"; // use require for mandatory files
require_once $base_path_to_learning_resources . 'lib/function.php';
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';

//valdation file
require_once $base_path_to_learning_resources . 'lib/validate_function.php';
require_once $base_path_to_learning . 'user_management/authenticate.php';

//dynemic db
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';

require_once $base_path_to_learning . "vendor/autoload.php";
require_once $base_path_to_learning_resources . 'lib/bucket_functions/bucket_functions.php';
require_once $base_path_to_learning_resources . 'includes/aws_bucket_config.php';
require_once $base_path_to_learning . 'functions/common_functions.php';
require_once $base_path_to_learning . 'functions/get_branch_functions.php';

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

if (
    !isset($postdata['institution_id']) or !checkIfIssetAndNotEmpty($postdata['institution_id']) or
    !isset($postdata['api_signature']) or !checkIfIssetAndNotEmpty($postdata['api_signature']) or
    !isset($postdata['function_type']) or !checkIfIssetAndNotEmpty($postdata['function_type']) or
    !isset($postdata['jwt_token']) or  !checkIfIssetAndNotEmpty($postdata['jwt_token'])
) {
    errorreturn("All * fields are required.");
}

validateApiSignature($postdata['api_signature']);
$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$username = $jwt_validate->data->username;
$role = $jwt_validate->data->role;

$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id, ['superadmin', 'admin'], null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}
$academic_id = CheckAcademicSession($conn_rjllearning_specific, $postdata['institution_id']);
if ($academic_id == false) {
    errorreturn("Access denied. Academic session not active.");
}


function get_Specific_Activity_Planner($conn_rjllearning_specific, $institution_id, $activity_id)
{
    $sql = "SELECT * FROM activity_planner WHERE institution_id = ? AND id = ?";
    $stmt = $conn_rjllearning_specific->prepare($sql);
    $stmt->execute([$institution_id, $activity_id]);
    $rows = $stmt->fetch();

    if (!$rows) {
        errorreturn("No activity found.");
    }

   
        if ($rows['branch_id'] == 'all' ) {
            $branch_info = array('branch_id' => 'all', 'branch_name' => 'All branches');
        } else {
            $branch_info = getBranchInfo($conn_rjllearning_specific, $institution_id, $rows['branch_id']);
        }
        
        // Class Info
        if ($rows['class_id'] == 'all' ) {
            $class_info = array('class_id' => 'all', 'class_name' => 'All Classes');
        } else {
            $class_info = getClass($conn_rjllearning_specific, $institution_id, $rows['class_id']); 
        }
        
        // Section Info
        if ($rows['section_id'] == 'all') {
            $section_info = array('section_id' => 'all', 'section_name' => 'All Sections');
        } else {
            $section_info = getSection($conn_rjllearning_specific, $institution_id, $rows['section_id']);
        }
        
        if ($rows['faculty_id'] == 'all' ) {
            $faculty_info = array('faculty_id' => 'all', 'name' => 'All Faculty');
        } else {
            $faculty_info = getFacultyInfo($conn_rjllearning_specific, $institution_id, $rows['faculty_id']);
        }
        // $subject_info = $rows['subject_id'] == 'all' ? array('subject_id' => 'all', 'name' => ucwords('all')) : 
        // getSubject($conn_rjllearning_specific, $institution_id, $rows['subject_id']);

        $schedule_details = json_decode($rows['schedule_details'], true);

        $array_return = array(
            'activty_planner_id' => $rows['id'],
            'institution_id' => $rows['institution_id'],
            'branch' => $branch_info,
            'faculty' => $faculty_info,
            'from_date' => $rows['from_date'],
            'to_date' => $rows['to_date'],
            'activity_name' => $rows['activity_name'],
            'activity_type' => $rows['activity_type'],
            'class_info' => $class_info, 
            'section_info' => $section_info, 
            // 'subject_info' => $subject_info, 
            'schedule_details' => $schedule_details[0], 
            'description' => $rows['description'], 
            'date_created' => $rows['date_created'],
            'created_by' => $rows['created_by'],
            'last_modified_by' => $rows['last_modified_by'],
            'last_modified_at' => $rows['last_modified_at'],
            'status' => $rows['status'],
        );
    

    return $array_return;
}


if ($postdata['function_type'] == "fetch_specific_planner_activity") {
    if (!isset($postdata['activity_planner_id'])) {
        errorreturn("Planner ID is required and must be numeric.");
    }

    $institution_id = $postdata['institution_id'];
    $planner_id = (int)$postdata['activity_planner_id'];

    $success['data'] = get_Specific_Activity_Planner($conn_rjllearning_specific, $institution_id, $planner_id);
    successreturn($success);
}
