package handlers

import (
	"net/http"

	"gauth-central/internal/services"

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

type PostTagHandler struct {
	postTagService *services.PostTagService
}

func NewPostTagHandler(postTagService *services.PostTagService) *PostTagHandler {
	return &PostTagHandler{postTagService: postTagService}
}

type CreatePostTagRequest struct {
	Tag      string `json:"tag" binding:"required"`
	IsActive *bool  `json:"is_active"`
}

type UpdatePostTagRequest struct {
	Tag      string `json:"tag"`
	IsActive *bool  `json:"is_active"`
}

// GetAllPostTags godoc
// @Summary Get all active post tags
// @Description Retrieve a list of active post tags
// @Tags post-tags
// @Produce json
// @Success 200 {array} models.PostTag
// @Failure 500 {object} map[string]string
// @Router /post-tags [get]
func (h *PostTagHandler) GetAllPostTags(c *gin.Context) {
	tags, err := h.postTagService.GetAllPostTags(true)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, tags)
}

// AdminGetAllPostTags godoc
// @Summary Get all post tags (Admin)
// @Description Retrieve a list of all post tags including inactive ones
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Success 200 {array} models.PostTag
// @Failure 500 {object} map[string]string
// @Router /admin/post-tags [get]
func (h *PostTagHandler) AdminGetAllPostTags(c *gin.Context) {
	tags, err := h.postTagService.GetAllPostTags(false)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, tags)
}

// CreatePostTag godoc
// @Summary Create a post tag (Admin)
// @Description Create a new post tag
// @Tags admin
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Param request body CreatePostTagRequest true "Post Tag Request"
// @Success 201 {object} models.PostTag
// @Failure 400 {object} map[string]string
// @Router /admin/post-tags [post]
func (h *PostTagHandler) CreatePostTag(c *gin.Context) {
	var req CreatePostTagRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	isActive := true
	if req.IsActive != nil {
		isActive = *req.IsActive
	}

	tag, err := h.postTagService.CreatePostTag(req.Tag, isActive)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, tag)
}

// UpdatePostTag godoc
// @Summary Update a post tag (Admin)
// @Description Update an existing post tag
// @Tags admin
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Param id path string true "Post Tag ID"
// @Param request body UpdatePostTagRequest true "Post Tag Request"
// @Success 200 {object} models.PostTag
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /admin/post-tags/{id} [put]
func (h *PostTagHandler) UpdatePostTag(c *gin.Context) {
	id := c.Param("id")
	var req UpdatePostTagRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	tag, err := h.postTagService.UpdatePostTag(id, req.Tag, req.IsActive)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, tag)
}

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

// GetPostTagByID godoc
// @Summary Get a post tag by ID (Admin)
// @Description Retrieve details of a specific post tag
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Param id path string true "Post Tag ID"
// @Success 200 {object} models.PostTag
// @Failure 404 {object} map[string]string
// @Router /admin/post-tags/{id} [get]
func (h *PostTagHandler) GetPostTagByID(c *gin.Context) {
	id := c.Param("id")
	tag, err := h.postTagService.GetPostTagByID(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, tag)
}
