<?php

// api/core/db.php
require_once __DIR__ . '/../config/database.php';

/**
 * =========================
 * BASIC QUERY HELPERS
 * =========================
 */
function fetchAll(string $sql, array $params = []): array
{
    $db = Database::getConnection();
    $stmt = $db->prepare($sql);
    $stmt->execute($params);
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

function fetchOne(string $sql, array $params = []): ?array
{
    $db = Database::getConnection();
    $stmt = $db->prepare($sql);
    $stmt->execute($params);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    return $row ?: null;
}

function execute(string $sql, array $params = []): bool
{
    $db = Database::getConnection();
    $stmt = $db->prepare($sql);
    return $stmt->execute($params);
}

function lastInsertId(): string
{
    return Database::getConnection()->lastInsertId();
}

/**
 * =========================
 * TRANSACTION HELPERS
 * =========================
 */
function dbBeginTransaction(): bool
{
    return Database::getConnection()->beginTransaction();
}

function dbCommit(): bool
{
    return Database::getConnection()->commit();
}

function dbRollback(): bool
{
    return Database::getConnection()->rollBack();
}

function dbInTransaction(): bool
{
    return Database::getConnection()->inTransaction();
}

/**
 * =========================
 * OTHER HELPERS
 * =========================
 */
function returnBasePath(): string
{
    // MUST CHANGE FOR HOSTING OR LOCAL DEVELOPMENT
    return padel_attachments_url();
}
