<?php
// db.php — Conexión PDO
const DB_HOST    = 'localhost';
const DB_NAME    = 'sitiosnetfan3net_fetra'; // ej: sitiosnetfan3net_loquesea
const DB_USER    = 'sitiosnetfan3net_fetra';
const DB_PASS    = 'xY9@Lm48#VcN2qZrtP6!dAaKjUeRwH1b';
const DB_CHARSET = 'utf8mb4';

function pdo(): PDO {
  static $pdo = null;
  if ($pdo instanceof PDO) return $pdo;

  $dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHARSET;
  $opts = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
  ];

  try {
    $pdo = new PDO($dsn, DB_USER, DB_PASS, $opts);
    $pdo->exec("SET NAMES '".DB_CHARSET."' COLLATE 'utf8mb4_unicode_ci'");
    return $pdo;
  } catch (Throwable $e) {
    http_response_code(500);
    exit('Error de conexión a la base de datos.');
  }
}
