<?php
require_once __DIR__ . '/config/database.php';

function sendError($message, $code = 500) {
    http_response_code($code);
    echo json_encode(['success' => false, 'error' => $message]);
    exit;
}

function sendSuccess($data = null, $message = '') {
    echo json_encode(['success' => true, 'data' => $data, 'message' => $message]);
}

$db = getDb();
$sqlFile = __DIR__ . '/api_tables.sql';

if (!file_exists($sqlFile)) {
    die("SQL file not found at $sqlFile\n");
}

$sql = file_get_contents($sqlFile);

// Split by semicolon, but be careful with multi-line statements
// A simple split might work if sentences are simple.
// For robust splitting, we can use a more complex regex or just run the whole thing if mysqli multi_query is used.

if ($db->multi_query($sql)) {
    do {
        /* store first result set */
        if ($result = $db->store_result()) {
            $result->free();
        }
        /* print divider */
        if ($db->more_results()) {
            // printf("-----------------\n");
        }
    } while ($db->next_result());
    echo "Database initialization successful.\n";
} else {
    echo "Database initialization failed: " . $db->error . "\n";
}
