<?php
ini_set('display_errors', 1); // Turn off in production
ini_set('memory_limit', '512M');
ini_set('max_execution_time', 300); // 5 minutes limit instead of infinite
ini_set('allow_url_fopen', '1');


// Check required fields
$requiredFields = ['institution_id', 'username', 'workspace_id', 'api_signature', 'role','user_id'];
foreach ($requiredFields as $field) {
    if (empty($_POST[$field])) {
        echo json_encode(['success' => false, 'message' => "Missing field: $field"]);
        exit;
    }
}

// Check uploaded file
if (!isset($_FILES['excelFile']) || $_FILES['excelFile']['error'] != 0) {
    echo json_encode(['success' => false, 'message' => "Excel file is missing or failed to upload"]);
    exit;
}


// Get inputs
$institution_id = $_POST['institution_id'] ?? '';
$username = $_POST['username'] ?? '';
$workspace_id = $_POST['workspace_id'] ?? '';
$api_signature = $_POST['api_signature'] ?? '';
$role = $_POST['role'] ?? '';
$file_tmp = $_FILES['excelFile']['tmp_name'] ?? '';
$user_id = $_POST['user_id'] ?? '';
$function_type = $_POST['function_type'] ?? '';

// Validate inputs
if (empty($institution_id) || empty($username) || empty($workspace_id) || empty($api_signature) || empty($role) || empty($function_type) || empty($file_tmp)) {
    echo json_encode(['success' => false, 'message' => 'All fields and file upload are required.']);
    exit;
}

// Map function type to PHP import files
$importFiles = [
    'transport' => '/home/rejoicebilldesk/public_html/rjllearning_development/resources/lib/common_modules/import/transport_data/import_buses_data.php',
    'bus_routes' => '/home/rejoicebilldesk/public_html/rjllearning_development/resources/lib/common_modules/import/transport_data/import_bus_routes.php',
    'students' => '/home/rejoicebilldesk/public_html/rjllearning_development/resources/lib/common_modules/import/student_data/import_students.php',
    'staff' => '/home/rejoicebilldesk/public_html/rjllearning_development/resources/lib/common_modules/import/faculty_data/excel/import_faculty_data.php',
    'bus_stops' => '/home/rejoicebilldesk/public_html/rjllearning_development/resources/lib/common_modules/import/transport_data/import_bus_stops.php',
    'left_students'=>'/home/rejoicebilldesk/public_html/rjllearning_development/resources/lib/common_modules/import/left_student_import/import_left_students.php',
    // Add more mappings as needed
];

if (!isset($importFiles[$function_type])) {
    echo json_encode(['success' => false, 'message' => "Invalid function type"]);
    exit;
}

// Prepare data to pass to the import file
global $_IMPORT_CONTEXT;
$_IMPORT_CONTEXT = [
    'file_tmp' => $file_tmp,
    'institution_id' => $institution_id,
    'username' => $username,
    'workspace_id' => $workspace_id,
    'api_signature' => $api_signature,
    'role' => $role,
    'user_id' => $user_id
];

// Run import script
try {
    // This array will collect inserted IDs; make sure the import script populates it
    $inserted_ids = include $importFiles[$function_type]; 

    echo json_encode([
        'success' => true,
        'message' => ucfirst(str_replace('_', ' ', $function_type)) . " import completed successfully",
        'inserted_ids' => $inserted_ids
    ]);
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => "Import failed: " . $e->getMessage()
    ]);
}
?>
