package handlers

import (
	"strconv"

	"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/logger"
	"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/models"
	"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
	"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/storage"
	"github.com/gin-gonic/gin"
)

type PostHandlers struct {
	repo   repositories.PostRepository
	logger *logger.Logger
}

func NewPostHandlers(repo repositories.PostRepository) *PostHandlers {
	return &PostHandlers{repo: repo, logger: logger.New(false)}
}

// GetPosts godoc
// @Summary Get all published posts
// @Description Get all posts
// @Tags posts
// @Accept  json
// @Produce  json
// @Success 200 {object} models.SuccessResponse{data=[]storage.PostReq}
// @Failure 404 {object} models.ErrorResponse "No Post found"
// @Failure 500 {object} models.ErrorResponse "Internal server error"
// @Failure 400 {object} models.ErrorResponse "Invalid ID format"
// @Router /posts [get]
func (h *PostHandlers) GetPosts(c *gin.Context) {
	var result []storage.PostReq
	result, err := h.repo.GetAll(c.Request.Context())
	if err != nil {
		h.logger.Error("error request: " + err.Error())
		models.Error(c, 500, "Internal server error", err.Error())
		return
	}
	if result == nil {
		models.Error(c, 404, "No Post found", "")
		return
	}
	h.logger.Info("200 OK GET /posts")
	models.Success(c, result)
}

// GetPost godoc
// @Summary Get post by id
// @Description Get post by id
// @Tags posts
// @Accept  json
// @Param id path int true "Post ID"
// @Produce  json
// @Success 200 {object} models.SuccessResponse{data=storage.PostReq}
// @Failure 400 {object} models.ErrorResponse "Invalid ID format"
// @Failure 404 {object} models.ErrorResponse "Post not found"
// @Failure 500 {object} models.ErrorResponse "Internal server error"
// @Router /posts/{id} [get]
func (h *PostHandlers) GetPost(c *gin.Context) {
	var result storage.PostReq
	last_id, err := h.repo.GetLastID(c.Request.Context())
	if err != nil {
		h.logger.Error("error request: " + err.Error())
		models.Error(c, 500, "Internal server error", err.Error())
		return
	}
	id_p := c.Param("id")
	id, err := strconv.Atoi(id_p)
	if err != nil {
		h.logger.Error("error request: " + err.Error())
		models.Error(c, 400, "Invalid ID format", err.Error())
		return
	}
	if id > last_id {
		models.Error(c, 404, "Post not found", "")
		return
	}
	result, err = h.repo.GetByID(c.Request.Context(), id)
	if err != nil {
		h.logger.Error("error request: " + err.Error())
		models.Error(c, 500, "Internal server error", err.Error())
		return
	}
	h.logger.Info("200 OK GET /posts/" + id_p)
	models.Success(c, result)

}

// CreatePost godoc
// @Summary Create post
// @Description Create new post
// @Tags posts
// @Accept json
// @Produce json
// @Param post body storage.PostCreate true "Post data"
// @Security Bearer
// @Success 200 {object} models.SuccessResponse{data=storage.Post}
// @Failure 400 {object} models.ErrorResponse "Invalid request"
// @Router /posts [post]
func (h *PostHandlers) CreatePost(c *gin.Context) {
	var req storage.PostCreate

	if err := c.ShouldBindJSON(&req); err != nil {
		models.Error(c, 400, "Invalid request", err.Error())
		return
	}

	post := storage.Post{
		Title:   req.Title,
		Content: req.Content,
		Tags:    req.Tags,
	}

	if err := h.repo.Create(c.Request.Context(), post); err != nil {
		models.Error(c, 500, "Internal server error", err.Error())
		return
	}

	models.Success(c, post)
}

// UpdatePost godoc
// @Summary Update post
// @Description Update post
// @Tags posts
// @Accept  json
// @Param id path int true "Post ID"
// @Param post body storage.PostCreate true "Post data"
// @Produce  json
// @Security Bearer
// @Success 200 {object} models.SuccessResponse{data=storage.PostCreate}
// @Failure 400 {object} models.ErrorResponse "Invalid ID format"
// @Failure 500 {object} models.ErrorResponse "Internal server error"
// Failure 404 {object} models.ErrorResponse "Post not found"
// @Router /posts/{id} [put]
func (h *PostHandlers) UpdatePost(c *gin.Context) {
	id_p := c.Param("id")
	id, err := strconv.Atoi(id_p)
	if err != nil {
		h.logger.Error("error request: " + err.Error())
		models.Error(c, 500, "Internal server error", err.Error())
		return
	}
	var req storage.Post
	if err := c.ShouldBindJSON(&req); err != nil {
		models.Error(c, 400, "Invalid request", err.Error())
		return
	}
	err = h.repo.Update(c.Request.Context(), id, req)
	if err != nil {
		models.Error(c, 500, "Internal server error", err.Error())
		return
	}

	models.Success(c, req)
	h.logger.Info("200 OK PUT /posts/" + id_p)
}

// DeletePost godoc
// @Summary Delete post
// @Description Delete post
// @Tags posts
// @Param id path int true "Post ID"
// @Accept  json
// @Produce  json
// @Security Bearer
// @Failure 404 {object} models.ErrorResponse "Post not found"
// @Failure 400 {object} models.ErrorResponse "Invalid ID format"
// @Failure 500 {object} models.ErrorResponse "Internal server error"
// @Success 200 {object} models.SuccessResponse{data=storage.Post}
// @Router /posts/{id} [delete]
func (h *PostHandlers) DeletePost(c *gin.Context) {
	id_p := c.Param("id")
	id, err := strconv.Atoi(id_p)
	if err != nil {
		h.logger.Error("error request: " + err.Error())
		models.Error(c, 400, "Invalid ID format", err.Error())
		return
	}
	exsist := h.repo.IsExist(c.Request.Context(), id)
	if !exsist {
		models.Error(c, 404, "Post not found", "")
		return
	}
	err = h.repo.Delete(c.Request.Context(), id)
	if err != nil {
		models.Error(c, 500, "Internal server error", err.Error())
		return
	}
	h.logger.Info("200 OK DELETE /posts/" + id_p)
	models.Success(c, "Post deleted")

}

// GetPosts godoc
// @Summary Get all posts
// @Description Get all posts
// @Tags admin
// @Security Bearer
// @Accept  json
// @Produce  json
// @Success 200 {object} models.SuccessResponse{data=[]storage.PostReq}
// @Failure 404 {object} models.ErrorResponse "No Post found"
// @Failure 500 {object} models.ErrorResponse "Internal server error"
// @Failure 400 {object} models.ErrorResponse "Invalid ID format"
// @Router /admin/posts [get]
func (h *PostHandlers) AdminGetAll(c *gin.Context) {
	var result []storage.PostReq
	result, err := h.repo.GetAllAdmin(c.Request.Context())
	if err != nil {
		h.logger.Error("error request: " + err.Error())
		models.Error(c, 500, "Internal server error", err.Error())
		return
	}
	if result == nil {
		models.Error(c, 404, "No Post found", "")
		return
	}
	h.logger.Info("200 OK GET /posts")
	models.Success(c, result)

}
