<?php
/**
 * Application Configuration
 * 
 * Dynamically fetches settings from the 'config' database table.
 * Fallback to default constants if database is not reachable.
 */

try {
    // Expecting $crud to be available from index.php
    if (isset($crud)) {
        $config_sql = "SELECT * FROM config LIMIT 1";
        $config_data = $crud->getRecordSet($config_sql);

        if (!empty($config_data)) {
            $conf = $config_data[0];
            define("M_Name", $conf['M_Name']);
            define("M_Address", $conf['M_Address']);
            define("L_Address", $conf['L_Address']);
            define("P_Number", $conf['P_Number']);
            define("Reg_Number", $conf['Reg_Number']);
            define("Elhaq", $conf['Elhaq']);
            define("Mohtamim", $conf['Mohtamim']);
            define("Phone", $conf['Phone']);
            define("Cell", $conf['Cell']);
            define("E_Address", $conf['E_Address']);
        }
    }
} catch (Exception $e) {
    // Error handling - constants won't be defined, but system shouldn't crash
    error_log("Configuration Load Error: " . $e->getMessage());
}

// Fallback safety (if DB is empty or fails)
if (!defined("M_Name"))
    define("M_Name", "مدرسہ کا نام یہاں لکھیں");
if (!defined("M_Address"))
    define("M_Address", "");
if (!defined("L_Address"))
    define("L_Address", "");
if (!defined("P_Number"))
    define("P_Number", "554545405405");
if (!defined("Reg_Number"))
    define("Reg_Number", "2/4875");
if (!defined("Elhaq"))
    define("Elhaq", "05879");
if (!defined("Mohtamim"))
    define("Mohtamim", "مہتمم صاحب کا نام");
if (!defined("Phone"))
    define("Phone", "665465465");
if (!defined("Cell"))
    define("Cell", "0321245874");
if (!defined("E_Address"))
    define("E_Address", "Address for Student Card.<br />with phone . Tel: 0946-262626");
?>