<?php
// ============================================
// NEXUS IT - Functions Library
// ============================================

// Get all active services
function getServices() {
    global $pdo;
    $stmt = $pdo->prepare("SELECT * FROM services WHERE is_active = 1 ORDER BY sort_order ASC");
    $stmt->execute();
    return $stmt->fetchAll();
}

// Get all active projects
function getProjects() {
    global $pdo;
    $stmt = $pdo->prepare("SELECT * FROM projects WHERE is_active = 1 ORDER BY sort_order ASC");
    $stmt->execute();
    return $stmt->fetchAll();
}

// Get all active why items
function getWhyItems() {
    global $pdo;
    $stmt = $pdo->prepare("SELECT * FROM why_items WHERE is_active = 1 ORDER BY sort_order ASC");
    $stmt->execute();
    return $stmt->fetchAll();
}

// Get unread messages count
function getUnreadMessagesCount() {
    global $pdo;
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM messages WHERE is_read = 0");
    $stmt->execute();
    return $stmt->fetchColumn();
}

// Get all messages
function getMessages() {
    global $pdo;
    $stmt = $pdo->prepare("SELECT * FROM messages ORDER BY created_at DESC");
    $stmt->execute();
    return $stmt->fetchAll();
}

// Save contact message
function saveMessage($data) {
    global $pdo;
    $stmt = $pdo->prepare("INSERT INTO messages (first_name, last_name, email, company, service, message) VALUES (?, ?, ?, ?, ?, ?)");
    return $stmt->execute([
        $data['first_name'],
        $data['last_name'],
        $data['email'],
        $data['company'] ?? '',
        $data['service'] ?? '',
        $data['message']
    ]);
}

// Update setting
function updateSetting($key, $value, $lang = 'en') {
    global $pdo;
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value, lang) VALUES (?, ?, ?) 
                           ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)");
    return $stmt->execute([$key, $value, $lang]);
}

// Upload image
function uploadImage($file, $prefix = 'img_') {
    if ($file['error'] !== UPLOAD_ERR_OK) {
        return ['success' => false, 'error' => 'Upload failed'];
    }
    
    if ($file['size'] > MAX_UPLOAD_SIZE) {
        return ['success' => false, 'error' => 'File too large'];
    }
    
    $allowed = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
    if (!in_array($file['type'], $allowed)) {
        return ['success' => false, 'error' => 'Invalid file type'];
    }
    
    $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
    $filename = $prefix . bin2hex(random_bytes(8)) . '.' . $ext;
    $destination = UPLOAD_DIR . $filename;
    
    if (!is_dir(UPLOAD_DIR)) {
        mkdir(UPLOAD_DIR, 0755, true);
    }
    
    if (move_uploaded_file($file['tmp_name'], $destination)) {
        return ['success' => true, 'filename' => $filename];
    }
    
    return ['success' => false, 'error' => 'Failed to save file'];
}

// Generate portfolio bg classes
function getPortfolioBgClasses() {
    return ['p1', 'p2', 'p3', 'p4', 'p5', 'p6'];
}
?>