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

function sanitizeInput($input) {
    if (is_array($input)) {
        return array_map('sanitizeInput', $input);
    }
    return htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8');
}

function formatDate($date) {
    return date('Y-m-d', strtotime($date));
}

function generateExportFilename($type) {
    return $type . '_' . date('Y-m-d') . '.json';
}

function ensureExportDirectory() {
    if (!file_exists(EXPORT_DESTINATION)) {
        mkdir(EXPORT_DESTINATION, 0755, true);
    }
}

/**
 * Base path prefix for JSON API under this app (e.g. /familyHub/api).
 */
function familyHubWebApiBase(): string {
    $sd = dirname($_SERVER['SCRIPT_NAME'] ?? '/');
    $sd = rtrim(str_replace('\\', '/', $sd), '/');
    if ($sd === '' || $sd === '/') {
        return '/api';
    }
    return $sd . '/api';
}
