<?php

function db_config(): array
{
    return [
        'host' => '127.0.0.1',
        'user' => 'root',
        'pass' => '',
        'name' => 'expenses',
    ];
}

function db(): mysqli
{
    static $conn = null;

    if ($conn instanceof mysqli) {
        return $conn;
    }

    mysqli_report(MYSQLI_REPORT_OFF);

    $config = db_config();
    $conn = new mysqli($config['host'], $config['user'], $config['pass'], $config['name']);

    if ($conn->connect_error) {
        die('Database connection failed: ' . $conn->connect_error);
    }

    $conn->set_charset('utf8mb4');

    return $conn;
}
