<?php
/**
 * DATABASE CONFIGURATION (multi-tenant)
 *
 * This app now serves MANY companies from ONE codebase. Each company has
 * its own separate database (created via the Super Admin panel). Which
 * database we connect to is decided per-request from the logged-in
 * user's session (set at login.php after routing through the master DB).
 *
 * Nothing else in the app needs to change - every module still just
 * uses the global $pdo, it now simply points at the correct company's
 * database automatically.
 */

// Application base settings
// IMPORTANT: update this to match wherever you deploy the app
// (e.g. 'https://adrieltech.com/seyalix' if you move it there)
define('APP_NAME', 'Seder Core');
define('APP_URL', 'https://adrieltech.com/sedercore');
define('APP_EDITION', 'full');   // 'full' or 'limited' - must match the `edition` this company is licensed for
define('UPLOAD_DIR', __DIR__ . '/../uploads/');

date_default_timezone_set('Asia/Kolkata');

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

$pdo = null;

if (!empty($_SESSION['tenant_db_name'])) {
    // A company user is logged in - connect to THEIR database.
    try {
        $pdo = new PDO(
            "mysql:host=" . $_SESSION['tenant_db_host'] . ";dbname=" . $_SESSION['tenant_db_name'] . ";charset=utf8mb4",
            $_SESSION['tenant_db_user'],
            $_SESSION['tenant_db_pass'],
            [
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_EMULATE_PREPARES => false,
            ]
        );
    } catch (PDOException $e) {
        die("Database connection failed: " . $e->getMessage());
    }
}
// If nobody is logged in yet (e.g. viewing login.php), $pdo stays null.
// login.php handles its own two-step connection (master DB, then tenant DB).
