<?php
// ============================================================
//  HealthLink – DB Configuration
//  File: config.php
// ============================================================

define('DB_HOST',     'localhost');
define('DB_USER',     'root');
define('DB_PASS',     '');
define('DB_NAME',     'healthlink_db');
define('DB_CHARSET',  'utf8mb4');

define('SITE_URL',    'http://localhost/Health_link');
define('SITE_NAME',   'HealthLink');
define('UPLOAD_PATH', __DIR__ . '/uploads/');

// Session start
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// DB Connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$conn->set_charset(DB_CHARSET);

if ($conn->connect_error) {
    die('<h3 style="color:red;font-family:sans-serif;">
        Database connection failed: ' . $conn->connect_error . '
        <br>config.php mein DB_USER aur DB_PASS check karo.
    </h3>');
}

// Helper Functions

function clean($data) {
    return htmlspecialchars(strip_tags(trim($data)));
}

function generateHealthId($conn) {
    $year = date('Y');
    $result = $conn->query("SELECT COUNT(*) as total FROM patients WHERE YEAR(created_at) = $year");
    $row = $result->fetch_assoc();
    $num = str_pad($row['total'] + 1, 5, '0', STR_PAD_LEFT);
    return "HL-{$year}-{$num}";
}

function generateAppointmentNo($conn) {
    $year = date('Y');
    $result = $conn->query("SELECT COUNT(*) as total FROM appointments WHERE YEAR(created_at) = $year");
    $row = $result->fetch_assoc();
    $num = str_pad($row['total'] + 1, 5, '0', STR_PAD_LEFT);
    return "APT-{$year}-{$num}";
}

function setFlash($type, $msg) {
    $_SESSION['flash'] = ['type' => $type, 'msg' => $msg];
}

function displayFlash() {
    if (!empty($_SESSION['flash'])) {
        $f = $_SESSION['flash'];
        $cls = $f['type'] === 'success' ? 'alert-success' : ($f['type'] === 'error' ? 'alert-danger' : 'alert-warning');
        echo "<div class='alert {$cls} alert-dismissible fade show' role='alert'>
                {$f['msg']}
                <button type='button' class='btn-close' data-bs-dismiss='alert'></button>
              </div>";
        unset($_SESSION['flash']);
    }
}

function requirePatient() {
    if (empty($_SESSION['patient_id'])) {
        header('Location: ' . SITE_URL . '/patient-login.php');
        exit;
    }
}

function requireDoctor() {
    if (empty($_SESSION['doctor_id'])) {
        header('Location: ' . SITE_URL . '/doctor-login.php');
        exit;
    }
}

function requireAdmin() {
    if (empty($_SESSION['admin_id'])) {
        header('Location: ' . SITE_URL . '/admin/login.php');
        exit;
    }
}

function uploadFile($file, $directory, $allowed_types = ['jpg','jpeg','png','pdf']) {
    if (empty($file['name'])) return null;
    
    $upload_dir = UPLOAD_PATH . $directory . '/';
    if (!is_dir($upload_dir)) mkdir($upload_dir, 0755, true);
    
    $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
    if (!in_array($ext, $allowed_types)) return false;
    
    $filename = time() . '_' . bin2hex(random_bytes(4)) . '.' . $ext;
    $destination = $upload_dir . $filename;
    
    if (move_uploaded_file($file['tmp_name'], $destination)) {
        return 'uploads/' . $directory . '/' . $filename;
    }
    return false;
}
?>