/**
 * Test Endpoint
 * GET /v1/example/test
 *
 * Example endpoint to demonstrate API structure and capabilities
 *
 * Query params:
 * - echo: text to echo back
 */

module.exports = async (req, res, next, slmapi) => {
    try {
        const response = {
            error: false,
            message: 'Test endpoint executed successfully',
            data: {
                api_info: {
                    name: slmapi.api_name,
                    version: slmapi.api_version,
                    uptime: process.uptime(),
                    environment: process.env.NODE_ENV || 'development'
                },
                request_info: {
                    method: req.method,
                    path: req.path,
                    params: req.params,
                    query: req.query,
                    ip: req.ip || req.connection.remoteAddress,
                    user_agent: req.get('user-agent')
                },
                elasticsearch: {
                    server: slmapi.es_server,
                    index: slmapi.es_index
                },
                features: {
                    authentication: true,
                    rate_limiting: slmapi.rate_limit_enabled,
                    hot_reload: true,
                    dynamic_routing: true
                },
                server_info: {
                    node_version: process.version,
                    platform: process.platform,
                    arch: process.arch,
                    pid: process.pid
                },
                date_examples: {
                    timestamp: slmapi.timestamp(),
                    now_utc: slmapi.now(),
                    now_mexico: slmapi.now('mx'),
                    formatted: slmapi.formatDate(Date.now(), 'DD/MM/YYYY HH:mm:ss'),
                    relative: slmapi.fromNow(Date.now() - 3600000), // 1 hora atrás
                    week_start: slmapi.moment().startOf('week').format('YYYY-MM-DD'),
                    week_end: slmapi.moment().endOf('week').format('YYYY-MM-DD'),
                    next_month: slmapi.moment().add(1, 'month').format('MMMM YYYY')
                }
            },
            timestamp: slmapi.timestamp()
        };

        // Si hay parámetro echo, agregarlo a la respuesta
        if (req.query.echo) {
            response.data.echo = req.query.echo;
        }

        return res.status(200).json(response);

    } catch (error) {
        console.error('Error in test endpoint:', error);
        return res.status(500).json({
            error: true,
            message: 'Internal server error',
            timestamp: slmapi.timestamp()
        });
    }
};
