<?php
/**
 * Chat Configuration File
 * Contains API endpoints and settings for real-time chat
 */

// Load environment variables from .env file
require_once __DIR__ . '/../config/env-loader.php';

// AI Service Configuration
define('OPENAI_API_KEY', getenv('OPENAI_API_KEY') ?: '');
define('OPENAI_MODEL', 'gpt-3.5-turbo');
define('OPENAI_MAX_TOKENS', 500);
define('OPENAI_TEMPERATURE', 0.7);

// Google Gemini Configuration (optional fallback)
define('GOOGLE_API_KEY', getenv('GOOGLE_API_KEY') ?: '');
define('GOOGLE_MODEL', 'gemini-pro');

// Chat Settings
define('CHAT_MAX_MESSAGE_LENGTH', 1000);
define('CHAT_RESPONSE_TIMEOUT', 30);
define('CHAT_ENABLE', true);
define('CHAT_STORE_CONVERSATIONS', true);
define('CHAT_ENABLE_TYPING_INDICATOR', true);

// API Endpoints
define('CHAT_API_BASE', '/auth-system/api/chat-ai.php');
define('CHAT_SEND_MESSAGE_ENDPOINT', '/auth-system/api/chat-ai.php?action=send_message');
define('CHAT_GET_MESSAGES_ENDPOINT', '/auth-system/api/chat-ai.php?action=get_messages');
define('CHAT_START_CHAT_ENDPOINT', '/auth-system/api/chat-ai.php?action=start_chat');
define('CHAT_STATUS_ENDPOINT', '/auth-system/api/chat-ai.php?action=get_status');

// Category Settings
$CHAT_CATEGORIES = [
    'service' => [
        'label' => 'Service',
        'icon' => 'wrench',
        'description' => 'Design services & consultation',
        'emoji' => '🔧'
    ],
    'project' => [
        'label' => 'Project',
        'icon' => 'project-diagram',
        'description' => 'Project planning & ideas',
        'emoji' => '📐'
    ],
    'order' => [
        'label' => 'Order',
        'icon' => 'shopping-cart',
        'description' => 'Order tracking & support',
        'emoji' => '📦'
    ],
    'contact' => [
        'label' => 'Contact',
        'icon' => 'envelope',
        'description' => 'General inquiries',
        'emoji' => '📧'
    ]
];

// Get AI service status
function getChatAIStatus()
{
    $has_openai = !empty(OPENAI_API_KEY);
    $has_google = !empty(GOOGLE_API_KEY);
    
    return [
        'openai_configured' => $has_openai,
        'google_configured' => $has_google,
        'primary_service' => $has_openai ? 'openai' : ($has_google ? 'google' : 'fallback'),
        'is_ready' => $has_openai || $has_google
    ];
}

// Return as JSON if requested
if (isset($_GET['format']) && $_GET['format'] === 'json') {
    header('Content-Type: application/json');
    echo json_encode([
        'success' => true,
        'config' => [
            'api_base' => CHAT_API_BASE,
            'max_message_length' => CHAT_MAX_MESSAGE_LENGTH,
            'response_timeout' => CHAT_RESPONSE_TIMEOUT,
            'typing_indicator' => CHAT_ENABLE_TYPING_INDICATOR,
            'ai_status' => getChatAIStatus(),
            'categories' => $CHAT_CATEGORIES
        ]
    ]);
    exit;
}
