package handlers

import (
	"net/http"
	"strconv"
	"time"

	"counter/internal/domain/entities"
	"counter/internal/usecase/counter"

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

// CounterHandler handles counter HTTP requests
type CounterHandler struct {
	counterService *counter.CounterService
}

// NewCounterHandler creates a new counter handler
func NewCounterHandler(counterService *counter.CounterService) *CounterHandler {
	return &CounterHandler{
		counterService: counterService,
	}
}

// Create handles counter creation
func (h *CounterHandler) Create(c *gin.Context) {
	userID := c.GetInt("user_id")

	var req counter.CreateCounterRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	counter, err := h.counterService.Create(c.Request.Context(), userID, &req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create counter"})
		return
	}

	c.JSON(http.StatusCreated, counter)
}

// Get handles retrieving a specific counter
func (h *CounterHandler) Get(c *gin.Context) {
	userID := c.GetInt("user_id")
	counterID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
		return
	}

	counter, err := h.counterService.Get(c.Request.Context(), counterID, userID)
	if err != nil {
		switch err {
		case entities.ErrCounterNotFound:
			c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
		default:
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch counter"})
		}
		return
	}

	c.JSON(http.StatusOK, counter)
}

// List handles retrieving all counters for a user
func (h *CounterHandler) List(c *gin.Context) {
	userID := c.GetInt("user_id")
	search := c.Query("search")

	counters, err := h.counterService.List(c.Request.Context(), userID, search)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch counters"})
		return
	}

	c.JSON(http.StatusOK, counters)
}

// Update handles counter updates
func (h *CounterHandler) Update(c *gin.Context) {
	userID := c.GetInt("user_id")
	counterID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
		return
	}

	var req counter.UpdateCounterRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	counter, err := h.counterService.Update(c.Request.Context(), counterID, userID, &req)
	if err != nil {
		switch err {
		case entities.ErrCounterNotFound:
			c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
		default:
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update counter"})
		}
		return
	}

	c.JSON(http.StatusOK, counter)
}

// Delete handles counter deletion
func (h *CounterHandler) Delete(c *gin.Context) {
	userID := c.GetInt("user_id")
	counterID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
		return
	}

	err = h.counterService.Delete(c.Request.Context(), counterID, userID)
	if err != nil {
		switch err {
		case entities.ErrCounterNotFound:
			c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
		default:
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete counter"})
		}
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Counter deleted successfully"})
}

// Increment handles counter increment/decrement
func (h *CounterHandler) Increment(c *gin.Context) {
	userID := c.GetInt("user_id")
	counterID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
		return
	}

	var req counter.IncrementRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	entry, err := h.counterService.Increment(c.Request.Context(), counterID, userID, &req)
	if err != nil {
		switch err {
		case entities.ErrCounterNotFound:
			c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
		default:
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create counter entry"})
		}
		return
	}

	c.JSON(http.StatusCreated, entry)
}

// GetEntries handles retrieving counter entries
func (h *CounterHandler) GetEntries(c *gin.Context) {
	userID := c.GetInt("user_id")
	counterID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
		return
	}

	// Parse date range parameters
	var startDate, endDate *time.Time
	if startDateStr := c.Query("start_date"); startDateStr != "" {
		if parsed, err := time.Parse("2006-01-02", startDateStr); err == nil {
			startDate = &parsed
		}
	}
	if endDateStr := c.Query("end_date"); endDateStr != "" {
		if parsed, err := time.Parse("2006-01-02", endDateStr); err == nil {
			endDate = &parsed
		}
	}

	entries, err := h.counterService.GetEntries(c.Request.Context(), counterID, userID, startDate, endDate)
	if err != nil {
		switch err {
		case entities.ErrCounterNotFound:
			c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
		default:
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch counter entries"})
		}
		return
	}

	c.JSON(http.StatusOK, entries)
}

// GetStats handles retrieving counter statistics
func (h *CounterHandler) GetStats(c *gin.Context) {
	userID := c.GetInt("user_id")
	counterID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
		return
	}

	// Parse days parameter
	days := 30 // default
	if daysStr := c.Query("days"); daysStr != "" {
		if parsed, err := strconv.Atoi(daysStr); err == nil && parsed > 0 {
			days = parsed
		}
	}

	stats, err := h.counterService.GetStats(c.Request.Context(), counterID, userID, days)
	if err != nil {
		switch err {
		case entities.ErrCounterNotFound:
			c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
		default:
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch statistics"})
		}
		return
	}

	c.JSON(http.StatusOK, gin.H{"daily_stats": stats})
}
