<?php

namespace App\Services;

class ReportService extends ApiService
{
    /**
     * Obtener lista de reportes disponibles
     */
    public function getAvailableReports(): array
    {
        $response = $this->makeApiRequest('GET', 'reports', [], [], 'get_available_reports');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al obtener reportes disponibles');
        }

        return $response['data'] ?? [];
    }

    /**
     * Generar reporte específico
     */
    public function generateReport(string $reportType, array $filters = []): array
    {
        $response = $this->makeApiRequest('POST', "reports/{$reportType}", $filters, [], 'generate_report');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al generar reporte');
        }

        return $response['data'] ?? [];
    }

    /**
     * Descargar reporte en formato específico
     */
    public function downloadReport(string $reportId, string $format = 'pdf'): string
    {
        $response = $this->makeApiRequest('GET', "reports/{$reportId}/download", [], ['format' => $format], 'download_report');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al descargar reporte');
        }

        return $response['data'] ?? '';
    }

    /**
     * Obtener histórico de reportes generados
     */
    public function getReportHistory(array $filters = []): array
    {
        $response = $this->makeApiRequest('GET', 'reports/history', [], $filters, 'get_report_history');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al obtener histórico de reportes');
        }

        return $response['data'] ?? [];
    }

    /**
     * Obtener estadísticas del dashboard
     */
    public function getDashboardStats(): array
    {
        $response = $this->makeApiRequest('GET', 'dashboard/stats', [], [], 'get_dashboard_stats');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al obtener estadísticas del dashboard');
        }

        return $response['data'] ?? [];
    }

    /**
     * Obtener métricas en tiempo real
     */
    public function getRealTimeMetrics(): array
    {
        $response = $this->makeApiRequest('GET', 'metrics/realtime', [], [], 'get_realtime_metrics');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al obtener métricas en tiempo real');
        }

        return $response['data'] ?? [];
    }
}