<?php
// =====================================================================
// Database Helper
// Provides the PDO Connection and shared Utility Functions for all
// API Endpoints. Include this File at the Top of every Endpoint.
// =====================================================================

require_once __DIR__ . '/init.php';


// ---------------------------------------------------------------------
// JSON Response
// Sends a JSON-encoded Response with the given HTTP Status Code and
// terminates the Script. Every API Endpoint uses this for Output.
// ---------------------------------------------------------------------
function json_response($data, $status_code = 200) {
    http_response_code($status_code);
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($data, JSON_UNESCAPED_UNICODE);
    exit;
}


// ---------------------------------------------------------------------
// Error Response
// Sends a standardized Error Response with a Message and HTTP Status
// Code. Uses json_response() internally for consistent Formatting.
// ---------------------------------------------------------------------
function error_response($message, $status_code = 400) {
    json_response(['error' => $message], $status_code);
}


// ---------------------------------------------------------------------
// Validate Required Fields
// Checks if all specified Fields exist in the given Data Array and are
// non-empty. Returns an Array of missing Field Names, or an empty
// Array if all Fields are present.
// ---------------------------------------------------------------------
function validate_required($data, $fields) {
    $missing = [];

    foreach ($fields as $field) {
        if (!isset($data[$field]) || trim($data[$field]) === '') {
            $missing[] = $field;
        }
    }

    return $missing;
}


// ---------------------------------------------------------------------
// Get POST Input
// Reads and trims all POST Parameters. Returns an associative Array.
// Falls back to JSON Request Body if no POST Data is present
// (for Clients that send JSON instead of Form Data).
// ---------------------------------------------------------------------
function get_input() {
    // Check for standard POST Form Data first
    if (!empty($_POST)) {
        return array_map('trim', $_POST);
    }

    // Fall back to JSON Request Body
    $json = file_get_contents('php://input');
    $data = json_decode($json, true);

    if (is_array($data)) {
        return array_map('trim', $data);
    }

    return [];
}


// ---------------------------------------------------------------------
// Get PDO Connection
// Returns the PDO Instance created in init.php. Wrapped in a Function
// to avoid global Variable Dependencies in Endpoint Files.
// ---------------------------------------------------------------------
function get_db() {
    global $pdo;

    if (!$pdo) {
        error_response('Database Connection failed.', 500);
    }

    return $pdo;
}