<?php

namespace Modules\Performance\Controllers;

use Gestoreasy\Core\Http\Request;
use Gestoreasy\Core\Http\Response;
use Gestoreasy\Core\Database\QueryLogger;
use Gestoreasy\Core\Cache\CacheService;
use Gestoreasy\Core\Queue\QueueService;

class PerformanceController {
    /**
     * Obtém queries registradas
     */
    public function getQueries(Request $req): Response {
        try {
            $limit = min(100, max(1, (int)($req->query['limit'] ?? 50)));
            $queries = QueryLogger::getQueries();
            $queries = array_slice($queries, -$limit);

            return Response::json(['data' => $queries]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    /**
     * Obtém queries lentas
     */
    public function getSlowQueries(Request $req): Response {
        try {
            $queries = QueryLogger::getSlowQueries();
            return Response::json(['data' => $queries]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    /**
     * Obtém estatísticas de queries
     */
    public function getQueryStats(Request $req): Response {
        try {
            $stats = QueryLogger::getStats();
            return Response::json(['data' => $stats]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    /**
     * Obtém queries agrupadas por SQL
     */
    public function getQueriesBySql(Request $req): Response {
        try {
            $queries = QueryLogger::getQueriesBySql();
            return Response::json(['data' => $queries]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    /**
     * Limpa queries registradas
     */
    public function clearQueries(Request $req): Response {
        try {
            QueryLogger::clear();
            return Response::json(['data' => ['success' => true]]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    /**
     * Obtém estatísticas do cache
     */
    public function getCacheStats(Request $req): Response {
        try {
            $cache = new CacheService();
            $stats = $cache->getStats();
            return Response::json(['data' => $stats]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    /**
     * Obtém estatísticas das filas
     */
    public function getQueueStats(Request $req): Response {
        try {
            $queue = new QueueService();
            $queueName = $req->query['queue'] ?? 'default';
            $stats = $queue->getQueueStats($queueName);
            return Response::json(['data' => $stats]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }
}

