<?php
/**
 * Point d'entrée de l'application
 * mySupport - Plateforme de Service Client Intelligent
 */

// Activer l'affichage des erreurs pour le debug
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Démarrer la session si pas déjà démarrée
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Autoload Composer
if (file_exists(dirname(__DIR__) . '/vendor/autoload.php')) {
    require_once dirname(__DIR__) . '/vendor/autoload.php';
}

// Autoload
require_once dirname(__DIR__) . '/config/database.php';
require_once dirname(__DIR__) . '/config/app.php';
if (file_exists(dirname(__DIR__) . '/config/twilio.php')) {
    require_once dirname(__DIR__) . '/config/twilio.php';
}
require_once dirname(__DIR__) . '/core/Database.php';
require_once dirname(__DIR__) . '/core/Model.php';
require_once dirname(__DIR__) . '/core/Controller.php';
require_once dirname(__DIR__) . '/core/Router.php';

use Core\Router;

$router = new Router();

// Routes publiques
$router->get('/', ['HomeController', 'index']);
$router->get('/auth/login', ['AuthController', 'loginForm']);
$router->post('/auth/login', ['AuthController', 'login']);
$router->get('/auth/logout', ['AuthController', 'logout']);
$router->get('/auth/register', ['AuthController', 'registerForm']);
$router->post('/auth/register', ['AuthController', 'register']);
$router->get('/auth/unauthorized', ['AuthController', 'unauthorized']);

// Routes Super Admin
$router->get('/super-admin', ['SuperAdminController', 'dashboard']);
$router->get('/super-admin/entreprises', ['SuperAdminController', 'entreprises']);
$router->get('/super-admin/entreprises/create', ['SuperAdminController', 'createEntreprise']);
$router->post('/super-admin/entreprises/store', ['SuperAdminController', 'storeEntreprise']);
$router->get('/super-admin/entreprises/{id}', ['SuperAdminController', 'editEntreprise']);
$router->post('/super-admin/entreprises/{id}/update', ['SuperAdminController', 'updateEntreprise']);
$router->post('/super-admin/entreprises/{id}/toggle', ['SuperAdminController', 'toggleEntreprise']);
$router->get('/super-admin/statistiques', ['SuperAdminController', 'statistiques']);
$router->get('/super-admin/langues', ['SuperAdminController', 'langues']);
$router->get('/super-admin/abonnements', ['SuperAdminController', 'abonnements']);

// Routes Admin Entreprise
$router->get('/admin', ['AdminController', 'dashboard']);
$router->get('/admin/agents', ['AdminController', 'agents']);
$router->get('/admin/agents/create', ['AdminController', 'createAgent']);
$router->post('/admin/agents/store', ['AdminController', 'storeAgent']);
$router->get('/admin/agents/{id}', ['AdminController', 'editAgent']);
$router->post('/admin/agents/{id}/update', ['AdminController', 'updateAgent']);
$router->post('/admin/agents/{id}/delete', ['AdminController', 'deleteAgent']);
$router->get('/admin/conversations', ['AdminController', 'conversations']);
$router->get('/admin/statistiques', ['AdminController', 'statistiques']);
$router->get('/admin/cx-index', ['AdminController', 'cxIndex']);
$router->get('/admin/alertes', ['AdminController', 'alertes']);
$router->get('/admin/parametres', ['AdminController', 'parametres']);
$router->post('/admin/parametres/update', ['AdminController', 'updateParametres']);
$router->get('/admin/integration', ['AdminController', 'integration']);
$router->post('/admin/integration/regenerate-key', ['AdminController', 'regenerateApiKey']);

// Routes IA & FAQ
$router->get('/admin/ia', ['AdminController', 'ia']);
$router->get('/admin/ia/faq/create', ['AdminController', 'createFaq']);
$router->post('/admin/ia/faq', ['AdminController', 'storeFaq']);
$router->get('/admin/ia/faq/{id}/edit', ['AdminController', 'editFaq']);
$router->post('/admin/ia/faq/{id}', ['AdminController', 'updateFaq']);
$router->post('/admin/ia/faq/{id}/delete', ['AdminController', 'deleteFaq']);
$router->post('/admin/ia/config', ['AdminController', 'updateIaConfig']);
$router->post('/admin/ia/documents', ['AdminController', 'uploadDocument']);
$router->post('/admin/ia/documents/{id}/delete', ['AdminController', 'deleteDocument']);
$router->post('/admin/ia/documents/{id}/toggle', ['AdminController', 'toggleDocument']);

// QR Codes & Liens directs
$router->get('/admin/qrcodes', ['AdminController', 'qrcodes']);

// Route Admin Conversations detail
$router->get('/admin/conversations/{id}', ['AdminController', 'viewConversation']);
$router->post('/admin/conversations/{id}/assigner', ['AdminController', 'assignerConversation']);
$router->post('/admin/conversations/{id}/statut', ['AdminController', 'updateStatutConversation']);

// Routes Agent
$router->get('/agent', ['AgentController', 'dashboard']);
$router->get('/agent/conversations', ['AgentController', 'conversations']);
$router->get('/agent/conversations/{id}', ['AgentController', 'viewConversation']);
$router->post('/agent/conversations/{id}/reply', ['AgentController', 'replyConversation']);
$router->post('/agent/conversations/{id}/close', ['AgentController', 'closeConversation']);
$router->post('/agent/conversations/{id}/transfer', ['AgentController', 'transferConversation']);

// Routes Agent - Tickets & Escalade
$router->get('/agent/tickets', ['AgentController', 'tickets']);
$router->get('/agent/tickets/{id}', ['AgentController', 'viewTicket']);
$router->post('/agent/tickets/{id}/take', ['AgentController', 'takeTicket']);
$router->post('/agent/tickets/{id}/status', ['AgentController', 'updateTicketStatus']);
$router->post('/agent/live-request/{id}/take', ['AgentController', 'takeLiveRequest']);
$router->get('/agent/api/notifications', ['AgentController', 'getNotifications']);
$router->post('/agent/api/notifications/read', ['AgentController', 'markNotificationsRead']);

// API Chat Widget
$router->post('/api/chat/start', ['ChatApiController', 'startConversation']);
$router->post('/api/chat/message', ['ChatApiController', 'sendMessage']);
$router->get('/api/chat/messages/{conversationId}', ['ChatApiController', 'getMessages']);
$router->post('/api/chat/close', ['ChatApiController', 'closeConversation']);
$router->post('/api/chat/tts', ['ChatApiController', 'synthesizeSpeech']);

// API Escalade (tickets et chat en direct)
$router->post('/api/chat/escalate/ticket', ['ChatApiController', 'createTicket']);
$router->post('/api/chat/escalate/live', ['ChatApiController', 'requestLiveChat']);
$router->get('/api/chat/escalate/status', ['ChatApiController', 'checkEscalationStatus']);
$router->get('/api/chat/escalate/options', ['ChatApiController', 'getEscalationOptions']);

// API CSAT (Enquête satisfaction)
$router->post('/api/chat/csat/submit', ['ChatApiController', 'submitCSAT']);
$router->get('/api/chat/conversation/status', ['ChatApiController', 'getConversationStatus']);
$router->get('/feedback/{token}', ['ChatApiController', 'feedbackPage']);
$router->post('/feedback/{token}', ['ChatApiController', 'submitFeedback']);

// API Push Notifications (PWA)
$router->post('/api/push/subscribe', ['PushController', 'subscribe']);
$router->post('/api/push/unsubscribe', ['PushController', 'unsubscribe']);
$router->get('/api/push/vapid-key', ['PushController', 'getVapidKey']);

// API Tags
$router->get('/api/tags/stats', ['TagController', 'getStats']);
$router->post('/api/conversation/{id}/tag', ['TagController', 'addTag']);
$router->delete('/api/conversation/{id}/tag/{tag}', ['TagController', 'removeTag']);

// API Dénonciations
$router->post('/api/denonciation/submit', ['DenonciationController', 'submit']);
$router->post('/api/denonciation/check', ['DenonciationController', 'checkEnabled']);

// Admin Dénonciations
$router->get('/admin/denonciations', ['DenonciationController', 'index']);
$router->get('/admin/denonciations/{id}', ['DenonciationController', 'viewDenonciation']);
$router->post('/admin/denonciations/{id}/status', ['DenonciationController', 'updateStatus']);
$router->post('/admin/denonciations/{id}/statut', ['DenonciationController', 'updateStatut']);
$router->post('/admin/denonciations/{id}/priorite', ['DenonciationController', 'updatePriorite']);
$router->post('/admin/denonciations/{id}/assigner', ['DenonciationController', 'assigner']);
$router->post('/admin/denonciations/{id}/note', ['DenonciationController', 'addNote']);
$router->post('/admin/denonciations/{id}/cloturer', ['DenonciationController', 'cloturer']);
$router->post('/admin/denonciations/{id}/piece', ['DenonciationController', 'addPiece']);
$router->post('/admin/denonciations/toggle', ['DenonciationController', 'toggleEnabled']);

// API Webhooks (WhatsApp, etc.)
$router->post('/api/webhook/whatsapp', ['WebhookController', 'whatsapp']);

// Twilio Voice Webhooks
$router->post('/api/twilio/voice', ['TwilioController', 'voice']);
$router->post('/api/twilio/gather', ['TwilioController', 'gather']);
$router->post('/api/twilio/recording', ['TwilioController', 'recording']);
$router->post('/api/twilio/voicemail', ['TwilioController', 'voicemail']);
$router->post('/api/twilio/voicemail-saved', ['TwilioController', 'voicemailSaved']);
$router->post('/api/twilio/transcription', ['TwilioController', 'transcription']);
$router->post('/api/twilio/dial-status', ['TwilioController', 'dialStatus']);
$router->post('/api/twilio/status', ['TwilioController', 'status']);

// Twilio WebRTC
$router->get('/api/twilio/token', ['TwilioController', 'getToken']);
$router->post('/api/twilio/token', ['TwilioController', 'getToken']);
$router->post('/api/twilio/webrtc-voice', ['TwilioController', 'webrtcVoice']);

// Servir le script embed.js (AVANT la route dynamique)
$router->get('/widget/embed.js', ['WidgetController', 'embedScript']);

// Widget Chat via API Key
$router->get('/widget/chat', ['WidgetController', 'chatByApiKey']);

// Widget Chat via Token
$router->get('/widget/{token}', ['WidgetController', 'index']);

$router->dispatch();
