getMessage(); } $action = $_GET['action'] ?? $_POST['action'] ?? 'stats'; $service_name = basename(__FILE__, '.php'); // Déterminer la table appropriée basée sur le nom du fichier $table_map = [ 'account-creator' => 'admin.office_accounts', 'ai-copywriter' => 'admin.ai_templates', 'bpms-command-center' => 'admin.bpms_flows', 'captcha-solver' => 'admin.captcha_services', 'cvc-vault' => 'admin.cvc_cards', 'data-manager' => 'admin.data_sources', 'fingerprint-sync' => 'admin.fingerprint_profiles', 'harvest-manager' => 'admin.harvest_jobs', 'lookalike-engine' => 'admin.lookalike_models', 'neural-dom-mutator' => 'admin.dom_mutations', 'pattern-shuffler' => 'admin.patterns', 'phone-generator' => 'admin.phone_numbers', 'scrapping-factory' => 'admin.scraping_jobs', 'seed-cleaner' => 'admin.seed_lists', 'self-healing' => 'admin.self_healing_logs', 'send-factory' => 'admin.send_jobs', 'sms-engine' => 'admin.sms_campaigns', 'sms-templates' => 'admin.sms_templates', 'temp-email' => 'admin.temp_email_accounts', 'trap-detector' => 'admin.trap_detections', 'world-map' => 'admin.geo_locations' ]; $table_name = $table_map[$service_name] ?? 'admin.unified_send_log'; switch($action) { case 'stats': if (!$db_connected) { echo json_encode([ 'status' => 'error', 'message' => 'Database connection failed', 'error' => $db_error, 'service' => $service_name, 'timestamp' => date('Y-m-d H:i:s') ]); break; } try { // Statistiques basiques $query = "SELECT COUNT(*) as total, COUNT(CASE WHEN status = 'active' THEN 1 END) as active, COUNT(CASE WHEN status = 'pending' THEN 1 END) as pending, COUNT(CASE WHEN status = 'error' THEN 1 END) as errors, MAX(created_at) as last_created, MIN(created_at) as first_created FROM $table_name"; $stmt = $pdo->query($query); $stats = $stmt->fetch(PDO::FETCH_ASSOC); if (!$stats) { // Si la table n'existe pas, retourner des stats par défaut $stats = [ 'total' => 0, 'active' => 0, 'pending' => 0, 'errors' => 0, 'last_created' => null, 'first_created' => null ]; } echo json_encode([ 'status' => 'success', 'data' => $stats, 'service' => $service_name, 'table' => $table_name, 'database' => $db_connected ? 'connected' : 'disconnected', 'timestamp' => date('Y-m-d H:i:s') ]); } catch (Exception $e) { // Table probablement inexistante, retourner des stats de base echo json_encode([ 'status' => 'success', 'data' => [ 'total' => 0, 'active' => 0, 'pending' => 0, 'errors' => 0, 'note' => 'Table may not exist, using default stats' ], 'service' => $service_name, 'warning' => 'Table query failed: ' . $e->getMessage(), 'timestamp' => date('Y-m-d H:i:s') ]); } break; case 'list': if (!$db_connected) { echo json_encode([ 'status' => 'error', 'message' => 'Database connection failed', 'service' => $service_name ]); break; } $limit = min(intval($_GET['limit'] ?? 10), 100); $offset = intval($_GET['offset'] ?? 0); try { $stmt = $pdo->prepare(" SELECT * FROM $table_name ORDER BY created_at DESC LIMIT :limit OFFSET :offset "); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->bindValue(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode([ 'status' => 'success', 'data' => $data, 'count' => count($data), 'service' => $service_name, 'limit' => $limit, 'offset' => $offset ]); } catch (Exception $e) { // Table inexistante ou erreur, retourner des données d'exemple $sample_data = []; for ($i = 1; $i <= min($limit, 5); $i++) { $sample_data[] = [ 'id' => $i, 'name' => "Sample $service_name item $i", 'status' => 'active', 'created_at' => date('Y-m-d H:i:s', strtotime("-$i hours")), 'note' => 'Sample data - table may not exist' ]; } echo json_encode([ 'status' => 'success', 'data' => $sample_data, 'count' => count($sample_data), 'service' => $service_name, 'warning' => 'Using sample data: ' . $e->getMessage(), 'limit' => $limit, 'offset' => $offset ]); } break; case 'health': $checks = [ 'database' => $db_connected, 'api_endpoint' => true, 'table_exists' => false, 'timestamp' => date('Y-m-d H:i:s') ]; if ($db_connected) { try { $stmt = $pdo->query("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'admin' AND table_name = '" . str_replace('admin.', '', $table_name) . "')"); $checks['table_exists'] = $stmt->fetchColumn(); } catch (Exception $e) { $checks['table_error'] = $e->getMessage(); } } else { $checks['database_error'] = $db_error; } echo json_encode([ 'status' => $checks['database'] ? ($checks['table_exists'] ? 'healthy' : 'degraded') : 'unhealthy', 'checks' => $checks, 'service' => $service_name, 'recommendation' => $checks['table_exists'] ? 'All good' : 'Consider creating table ' . $table_name ]); break; default: echo json_encode([ 'status' => 'error', 'message' => 'Unknown action', 'available_actions' => ['stats', 'list', 'health'], 'service' => $service_name, 'documentation' => '/api/' . $service_name . '.php' ]); } ?>