package handlers

import (
	"net/http"
	"strconv"

	"gauth-central/internal/services"

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

type ContactHandler struct {
	contactService *services.ContactService
}

func NewContactHandler(contactService *services.ContactService) *ContactHandler {
	return &ContactHandler{contactService: contactService}
}

type CreateContactRequest struct {
	Name    string `json:"name" binding:"required"`
	Email   string `json:"email" binding:"required,email"`
	Subject string `json:"subject" binding:"required"`
	Message string `json:"message" binding:"required"`
}

// CreateContact godoc
// @Summary Create a new contact message
// @Description Send a contact message
// @Tags contact
// @Accept json
// @Produce json
// @Param request body CreateContactRequest true "Contact Request"
// @Success 201 {object} models.Contact
// @Failure 400 {object} map[string]string
// @Router /contact [post]
func (h *ContactHandler) CreateContact(c *gin.Context) {
	var req CreateContactRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// Get IP address
	ip := c.ClientIP()

	// Get User ID if authenticated (optional)
	var userID *string
	if id, exists := c.Get("user_id"); exists {
		idStr := id.(string)
		userID = &idStr
	}

	contact, err := h.contactService.CreateContact(req.Name, req.Email, req.Subject, req.Message, ip, userID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, contact)
}

// GetAllContacts godoc
// @Summary Get all contact messages (Admin)
// @Description Retrieve a list of all contact messages with pagination
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Param page query int false "Page number"
// @Param limit query int false "Items per page"
// @Success 200 {object} map[string]interface{}
// @Failure 500 {object} map[string]string
// @Router /admin/contacts [get]
func (h *ContactHandler) GetAllContacts(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))

	contacts, total, err := h.contactService.GetAllContacts(page, limit)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"data":  contacts,
		"total": total,
		"page":  page,
		"limit": limit,
	})
}

// GetContactByID godoc
// @Summary Get a contact message by ID (Admin)
// @Description Retrieve details of a specific contact message
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Param id path string true "Contact ID"
// @Success 200 {object} models.Contact
// @Failure 404 {object} map[string]string
// @Router /admin/contacts/{id} [get]
func (h *ContactHandler) GetContactByID(c *gin.Context) {
	id := c.Param("id")
	contact, err := h.contactService.GetContactByID(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, contact)
}

// DeleteContact godoc
// @Summary Delete a contact message (Admin)
// @Description Delete a contact message by ID
// @Tags admin
// @Security ApiKeyAuth
// @Param id path string true "Contact ID"
// @Success 200 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /admin/contacts/{id} [delete]
func (h *ContactHandler) DeleteContact(c *gin.Context) {
	id := c.Param("id")
	if err := h.contactService.DeleteContact(id); err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "Contact deleted successfully"})
}
