<?php

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// MySQL connection
$host = "localhost";
$db   = "simple_login";
$user = "root";
$pass = "";

try {
    $pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
    die("Koneksi gagal: " . $e->getMessage());
}

/**
 * Proteksi: buat fungsi hanya jika belum ada.
 * Ini mencegah "Cannot redeclare" bila file ini di-include lebih dari sekali.
 */
if (!function_exists('is_logged_in')) {
    function is_logged_in() {
        return isset($_SESSION['user_id']);
    }
}

if (!function_exists('is_admin')) {
    function is_admin() {
        return (isset($_SESSION['role']) && $_SESSION['role'] === 'admin');
    }
}

if (!function_exists('require_login')) {
    function require_login() {
        if (!is_logged_in()) {
            header('Location: login.php');
            exit;
        }
    }
}

/* -----------------------------------------
   🚀 WhatsApp Community
------------------------------------------ */

$whatsapp_community_link = "https://chat.whatsapp.com/DQuvVD5dubuJnMLsoYLsY0?mode=wwt";
$whatsapp_community_name = "PustakaIA";

/* -----------------------------------------
   ✉️ SMTP (Email) untuk kirim reset password
   (Pastikan APP PASSWORD yang kamu isi adalah App Password Google)
------------------------------------------ */

if (!defined('SMTP_HOST')) define('SMTP_HOST', 'smtp.gmail.com');
if (!defined('SMTP_USER')) define('SMTP_USER', 'sempurane2023@gmail.com');
if (!defined('SMTP_PASS')) define('SMTP_PASS', 'vqdyjdwcmgvepzmc'); // contoh: app password 16 char (tanpa spasi)
if (!defined('SMTP_PORT')) define('SMTP_PORT', 587);
if (!defined('SMTP_SECURE')) define('SMTP_SECURE', 'tls');
