package handler

import (
	"net/http"

	"eslogad-be/internal/contract"
	"eslogad-be/internal/service"

	"github.com/gin-gonic/gin"
)

type AnalyticsHandler struct {
	analyticsService service.AnalyticsService
}

func NewAnalyticsHandler(analyticsService service.AnalyticsService) *AnalyticsHandler {
	return &AnalyticsHandler{
		analyticsService: analyticsService,
	}
}

// GetDashboard handles GET /api/v1/analytics/dashboard
func (h *AnalyticsHandler) GetDashboard(c *gin.Context) {
	var req contract.AnalyticsDashboardRequest
	
	// Bind query parameters
	if err := c.ShouldBindQuery(&req); err != nil {
		c.JSON(http.StatusBadRequest, &contract.ErrorResponse{
			Error: "Invalid query parameters",
			Code:  http.StatusBadRequest,
		})
		return
	}

	// Get analytics dashboard data
	response, err := h.analyticsService.GetDashboard(c.Request.Context(), &req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, &contract.ErrorResponse{
			Error: err.Error(),
			Code:  http.StatusInternalServerError,
		})
		return
	}

	c.JSON(http.StatusOK, contract.BuildSuccessResponse(response))
}

// GetLetterVolume handles GET /api/v1/analytics/volume
func (h *AnalyticsHandler) GetLetterVolume(c *gin.Context) {
	response, err := h.analyticsService.GetLetterVolume(c.Request.Context())
	if err != nil {
		c.JSON(http.StatusInternalServerError, &contract.ErrorResponse{
			Error: err.Error(),
			Code:  http.StatusInternalServerError,
		})
		return
	}

	c.JSON(http.StatusOK, contract.BuildSuccessResponse(response))
}

// GetStatusDistribution handles GET /api/v1/analytics/status-distribution
func (h *AnalyticsHandler) GetStatusDistribution(c *gin.Context) {
	var req contract.AnalyticsDashboardRequest
	
	if err := c.ShouldBindQuery(&req); err != nil {
		c.JSON(http.StatusBadRequest, &contract.ErrorResponse{
			Error: "Invalid query parameters",
			Code:  http.StatusBadRequest,
		})
		return
	}

	// Get full dashboard and extract status distribution
	response, err := h.analyticsService.GetDashboard(c.Request.Context(), &req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, &contract.ErrorResponse{
			Error: err.Error(),
			Code:  http.StatusInternalServerError,
		})
		return
	}

	c.JSON(http.StatusOK, contract.BuildSuccessResponse(map[string]interface{}{
		"status_distribution": response.StatusDistribution,
	}))
}

// GetPriorityDistribution handles GET /api/v1/analytics/priority-distribution
func (h *AnalyticsHandler) GetPriorityDistribution(c *gin.Context) {
	var req contract.AnalyticsDashboardRequest
	
	if err := c.ShouldBindQuery(&req); err != nil {
		c.JSON(http.StatusBadRequest, &contract.ErrorResponse{
			Error: "Invalid query parameters",
			Code:  http.StatusBadRequest,
		})
		return
	}

	// Get full dashboard and extract priority distribution
	response, err := h.analyticsService.GetDashboard(c.Request.Context(), &req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, &contract.ErrorResponse{
			Error: err.Error(),
			Code:  http.StatusInternalServerError,
		})
		return
	}

	c.JSON(http.StatusOK, contract.BuildSuccessResponse(map[string]interface{}{
		"priority_distribution": response.PriorityDistribution,
	}))
}

// GetDepartmentStats handles GET /api/v1/analytics/department-stats
func (h *AnalyticsHandler) GetDepartmentStats(c *gin.Context) {
	var req contract.AnalyticsDashboardRequest
	
	if err := c.ShouldBindQuery(&req); err != nil {
		c.JSON(http.StatusBadRequest, &contract.ErrorResponse{
			Error: "Invalid query parameters",
			Code:  http.StatusBadRequest,
		})
		return
	}

	// Get full dashboard and extract department stats
	response, err := h.analyticsService.GetDashboard(c.Request.Context(), &req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, &contract.ErrorResponse{
			Error: err.Error(),
			Code:  http.StatusInternalServerError,
		})
		return
	}

	c.JSON(http.StatusOK, contract.BuildSuccessResponse(map[string]interface{}{
		"department_stats": response.DepartmentStats,
	}))
}

// GetMonthlyTrend handles GET /api/v1/analytics/monthly-trend
func (h *AnalyticsHandler) GetMonthlyTrend(c *gin.Context) {
	var req contract.AnalyticsDashboardRequest
	
	if err := c.ShouldBindQuery(&req); err != nil {
		c.JSON(http.StatusBadRequest, &contract.ErrorResponse{
			Error: "Invalid query parameters",
			Code:  http.StatusBadRequest,
		})
		return
	}

	// Get full dashboard and extract monthly trend
	response, err := h.analyticsService.GetDashboard(c.Request.Context(), &req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, &contract.ErrorResponse{
			Error: err.Error(),
			Code:  http.StatusInternalServerError,
		})
		return
	}

	c.JSON(http.StatusOK, contract.BuildSuccessResponse(map[string]interface{}{
		"monthly_trend": response.MonthlyTrend,
	}))
}

// GetApprovalMetrics handles GET /api/v1/analytics/approval-metrics
func (h *AnalyticsHandler) GetApprovalMetrics(c *gin.Context) {
	var req contract.AnalyticsDashboardRequest
	
	if err := c.ShouldBindQuery(&req); err != nil {
		c.JSON(http.StatusBadRequest, &contract.ErrorResponse{
			Error: "Invalid query parameters",
			Code:  http.StatusBadRequest,
		})
		return
	}

	// Get full dashboard and extract approval metrics
	response, err := h.analyticsService.GetDashboard(c.Request.Context(), &req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, &contract.ErrorResponse{
			Error: err.Error(),
			Code:  http.StatusInternalServerError,
		})
		return
	}

	c.JSON(http.StatusOK, contract.BuildSuccessResponse(map[string]interface{}{
		"approval_metrics": response.ApprovalMetrics,
	}))
}