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

// Required files
require_once '../../../../resources/base_path_include.php';
require_once $base_path_to_learning_resources . "includes/connect.php";
require_once $base_path_to_learning_resources . 'lib/function.php';
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';
require_once $base_path_to_learning_resources . 'lib/validate_function.php';
require_once $base_path_to_learning . 'user_management/authenticate.php';
require_once $base_path_to_learning_resources . 'external_requests/finware_curl/groups_ledgers/curl_get_specific_student_ledgers_finware.php';
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';

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

// Validate API signature
validateCurlSignature($postdata['api_signature']);

$data = $postdata['fees_data'];
$workspace_id = $data['workspace_id'];
$branch_id = $data['branch_id'];
$role = $data['role'];
$userid = $data['userid'];

// Fetch workspace information
$sql = "SELECT db_name FROM workspace_database_info WHERE workspace_id = ? AND userid = ? AND status = ?";
$fetch_workspace = $conn_rjllearning->prepare($sql);
$fetch_workspace->execute(array($workspace_id, $userid, 1));
$result_workspace = $fetch_workspace->fetchAll();

// Return error if no workspace found
if (count($result_workspace) < 1) {
    echo json_encode(['error' => 'Fatal error (1). Access denied.']);
    die();
}

// Specific DB connection
$specific_db_name = $result_workspace[0]['db_name'];
$conn_rjllearning_specific = dynamicDbConnection($specific_db_name);

// Fetch branch details
$sqlbranch = $conn_rjllearning_specific->prepare("SELECT * FROM branch WHERE id = ? AND status = ?");
$sqlbranch->execute(array($branch_id, '1'));
$result_branch = $sqlbranch->fetchAll();

// Return error if no branch found
if (count($result_branch) < 1) {
    echo json_encode(['error' => 'Branch Not Found.']);
    die();
}

$institution_id = $result_branch[0]['institution_id'];

//active academic session
$sqlacademicsession = $conn_rjllearning_specific->prepare("
    SELECT id, name 
    FROM academic_session 
    WHERE institution_id = ? AND active_academic_session = ?
");
$sqlacademicsession->execute(array($institution_id, '1'));
$result_academic_session = $sqlacademicsession->fetch();

// Return error if no academic session is found
if (!$result_academic_session) {
    echo json_encode(['error' => 'Academic Session Not Found.']);
    die();
}

// Prepare academic session data
$academic_session = [
    'academic_id' => $result_academic_session['id'],
    'academic_session_name' => $result_academic_session['name']
];


// Fetch class details
$sqlclass = $conn_rjllearning_specific->prepare("
    SELECT id, classname 
    FROM class_info 
    WHERE branch_id LIKE ? AND institution_id = ? AND status = ?
");
$sqlclass->execute(array('%' . $branch_id . '%', $institution_id, '1'));
$result_class = $sqlclass->fetchAll();

// Return error if no classes found
if (count($result_class) < 1) {
    echo json_encode(['error' => 'Class Not Found.']);
    die();
}

// Prepare class_info as an array of objects
$class_info = [];
foreach ($result_class as $class) {
    $class_info[] = [
        'class_id' => $class['id'],
        'class_name' => $class['classname']
    ];
}

// Fetch section details
$sqlsection = $conn_rjllearning_specific->prepare("
    SELECT id, section_name 
    FROM section_info 
    WHERE branch_id LIKE ? AND institution_id = ? AND status = ?
");
$sqlsection->execute(array('%' . $branch_id . '%', $institution_id, '1'));
$result_section = $sqlsection->fetchAll();

// Return error if no sections found
if (count($result_section) < 1) {
    echo json_encode(['error' => 'Section Not Found.']);
    die();
}

// Prepare section_info as an array of objects
$section_info = [];
foreach ($result_section as $section) {
    $section_info[] = [
        'section_id' => $section['id'],
        'section_name' => $section['section_name']
    ];
}

// Prepare the success response
$data = [
    'academic_session' => $academic_session,
    'class_info' => $class_info,
    'section_info' => $section_info
];

echo json_encode($data);
die();