<?php
// /max7/config/support_config.php - Unified configuration for both admin and client

// Detect if we're in admin or client
define('IS_ADMIN', strpos($_SERVER['SCRIPT_NAME'], '/admin7/') !== false);

// Base paths
define('BASE_PATH', dirname(__DIR__, 2)); // Project root
define('UPLOAD_BASE', BASE_PATH . '/uploads/support/');
define('UPLOAD_URL', '/uploads/support/');

// Create upload directory if not exists
if (!file_exists(UPLOAD_BASE)) {
    mkdir(UPLOAD_BASE, 0755, true);
}

// Helper function for file upload
function uploadSupportFile($file, $ticket_id, $message_id = null) {
    if (!isset($file) || $file['error'] !== UPLOAD_ERR_OK) {
        return null;
    }
    
    $allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'application/pdf'];
    $max_size = 5 * 1024 * 1024; // 5MB
    
    if (!in_array($file['type'], $allowed_types)) {
        return ['error' => 'نوع الملف غير مسموح'];
    }
    
    if ($file['size'] > $max_size) {
        return ['error' => 'حجم الملف كبير جداً (الحد الأقصى 5MB)'];
    }
    
    $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
    $filename = time() . '_' . $ticket_id . '_' . uniqid() . '.' . $ext;
    $target = UPLOAD_BASE . $filename;
    
    if (move_uploaded_file($file['tmp_name'], $target)) {
        return ['success' => true, 'filename' => $filename, 'url' => UPLOAD_URL . $filename];
    }
    
    return ['error' => 'فشل في رفع الملف'];
}

// Helper function to get image URL
function getSupportImageUrl($filename) {
    if (empty($filename)) return '';
    return UPLOAD_URL . $filename;
}

// Database connection (use your existing config)
require_once BASE_PATH . '/max7/config.php';
?>