true, 'message' => 'API funcionando correctamente', 'timestamp' => date('Y-m-d H:i:s'), 'method' => $method ]); exit; } switch ($method) { case 'GET': switch ($action) { case 'status': // Estado general del sistema $allConfigs = getAllConfigsFromDB(); $whatsappValidation = validateWhatsAppConfigFromDB(); echo json_encode([ 'success' => true, 'data' => [ 'total_configs' => count($allConfigs), 'whatsapp_configured' => $whatsappValidation['valid'], 'database_migrated' => !empty($allConfigs), 'installation_completed' => isInstallationCompleted(), 'server_time' => date('Y-m-d H:i:s') ] ]); break; case 'all': // Obtener todas las configuraciones $configs = getAllConfigsFromDB(); echo json_encode([ 'success' => true, 'data' => $configs ]); break; case 'whatsapp': // Obtener configuración de WhatsApp $whatsappConfig = getWhatsAppConfigFromDB(); echo json_encode([ 'success' => true, 'data' => $whatsappConfig ]); break; case 'test_db': // Test de conexión a BD try { $pdo = createDbConnection(); $tableExists = $pdo->query("SHOW TABLES LIKE 'system_config'")->rowCount() > 0; echo json_encode([ 'success' => true, 'data' => [ 'db_connected' => true, 'table_exists' => $tableExists, 'config_count' => $tableExists ? $pdo->query("SELECT COUNT(*) FROM system_config")->fetchColumn() : 0 ] ]); } catch (Exception $e) { echo json_encode([ 'success' => false, 'error' => 'Error de BD: ' . $e->getMessage() ]); } break; default: throw new Exception('Acción GET no reconocida: ' . $action); } break; case 'POST': $input = json_decode(file_get_contents('php://input'), true); switch ($action) { case 'save_test': // Guardar configuración de prueba $key = 'api_test_' . time(); $value = 'Test desde API - ' . date('Y-m-d H:i:s'); $result = saveConfigToDB($key, $value, 'Prueba desde API'); echo json_encode([ 'success' => $result, 'message' => $result ? 'Configuración guardada correctamente' : 'Error guardando configuración', 'data' => [ 'key' => $key, 'value' => $value ] ]); break; case 'save_whatsapp_test': // Guardar config de WhatsApp de prueba $testConfig = [ 'token' => 'test_token_' . time(), 'phone_number_id' => '123456789', 'webhook_verify_token' => 'webhook_' . time() ]; $result = saveWhatsAppConfigToDB($testConfig); echo json_encode([ 'success' => $result, 'message' => $result ? 'WhatsApp config guardada' : 'Error guardando WhatsApp config', 'data' => $testConfig ]); break; default: throw new Exception('Acción POST no reconocida: ' . $action); } break; default: throw new Exception('Método HTTP no permitido: ' . $method); } } catch (Exception $e) { http_response_code(500); echo json_encode([ 'success' => false, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); } ?>