<?php
// index.php
require_once 'config.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';

$current_host = strtolower($_SERVER['HTTP_HOST'] ?? '');
// Normalize host: strip leading www.
$norm_host = preg_replace('/^www\./i', '', $current_host);

// 1. Check if this host is a registered user domain (Subdomain/Custom Domain Routing)
$stmt = $pdo->prepare("
    SELECT id FROM user_domains 
    WHERE LOWER(domain_name) = ? AND status IN ('verified', 'active')
    LIMIT 1
");
$stmt->execute([$norm_host]);
if ($stmt->fetch()) {
    // If it's a user domain or a root domain pointing here, let store.php handle rendering
    require 'store.php';
    exit;
}

// 2. Normal SonicPesa admin routing for the main domain
if (is_logged_in()) {
    redirect('dashboard');
} else {
    redirect('login');
}
?>

