package controller

import (
	"strconv"
	"strings"

	"github.com/gofiber/fiber/v2"

	"web-qudo-be/app/module/popup_news_content_images/request"
	"web-qudo-be/app/module/popup_news_content_images/service"

	utilRes "web-qudo-be/utils/response"
	utilVal "web-qudo-be/utils/validator"
)

type popupNewsContentImagesController struct {
	service service.PopupNewsContentImagesService
}

type PopupNewsContentImagesController interface {
	Save(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewPopupNewsContentImagesController(s service.PopupNewsContentImagesService) PopupNewsContentImagesController {
	return &popupNewsContentImagesController{
		service: s,
	}
}

// Save Popup News Content Image
// @Summary      Upload Popup News Content Image
// @Description  API for uploading image for Popup News Content
// @Tags         Popup News Content Images
// @Security     Bearer
// @Body 	     request.PopupNewsContentImagesCreateRequest
// @Success      200  {object}  response.Response
// @Failure      401  {object}  response.Response
// @Failure      422  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /popup-news-content-images [post]
func (_i *popupNewsContentImagesController) Save(c *fiber.Ctx) error {
	if strings.HasPrefix(c.Get("Content-Type"), "multipart/form-data") {
		cidStr := c.FormValue("popup_news_content_id")
		cid64, err := strconv.ParseUint(cidStr, 10, 32)
		if err != nil {
			return err
		}
		file, err := c.FormFile("file")
		if err != nil {
			return err
		}
		var isThumb *bool
		if v := strings.TrimSpace(c.FormValue("is_thumbnail")); v != "" {
			b := v == "true" || v == "1" || v == "on"
			isThumb = &b
		}
		result, err := _i.service.SaveWithFile(uint(cid64), file, isThumb)
		if err != nil {
			return err
		}
		return utilRes.Resp(c, utilRes.Response{
			Success:  true,
			Messages: utilRes.Messages{"Popup news content image successfully uploaded"},
			Data:     result,
		})
	}

	req := new(request.PopupNewsContentImagesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	err := _i.service.Save(*req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Popup news content image successfully uploaded"},
	})
}

// Delete Popup News Content Image
// @Summary      Delete Popup News Content Image
// @Description  API for deleting image
// @Tags         Popup News Content Images
// @Security     Bearer
// @Param        id path int true "Image ID"
// @Success      200  {object}  response.Response
// @Failure      401  {object}  response.Response
// @Failure      404  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /popup-news-content-images/{id} [delete]
func (_i *popupNewsContentImagesController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	err = _i.service.Delete(uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Messages: utilRes.Messages{"Popup news content image successfully deleted"},
	})
}