package controller

import (
	"jaecoo-be/app/module/galleries/request"
	"jaecoo-be/app/module/galleries/service"
	"jaecoo-be/utils/paginator"
	"strconv"

	"github.com/gofiber/fiber/v2"

	utilRes "jaecoo-be/utils/response"
	utilVal "jaecoo-be/utils/validator"
)

type galleriesController struct {
	galleriesService service.GalleriesService
}

type GalleriesController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewGalleriesController(galleriesService service.GalleriesService) GalleriesController {
	return &galleriesController{
		galleriesService: galleriesService,
	}
}

// All Galleries
// @Summary      Get all Galleries
// @Description  API for getting all Galleries
// @Tags         Galleries
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param 	     req query request.GalleriesQueryRequestContext false "query parameters"
// @Param 	     req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /galleries [get]
func (_i *galleriesController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.GalleriesQueryRequestContext{
		Title: c.Query("title"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

	galleriesData, paging, err := _i.galleriesService.GetAll(req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Galleries list successfully retrieved"},
		Data:     galleriesData,
		Meta:     paging,
	})
}

// Show Gallery
// @Summary      Get Gallery by ID
// @Description  API for getting Gallery by ID
// @Tags         Galleries
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        id path int true "Gallery ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /galleries/{id} [get]
func (_i *galleriesController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	galleryData, err := _i.galleriesService.GetOne(uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Gallery successfully retrieved"},
		Data:     galleryData,
	})
}

// Save Gallery
// @Summary      Create Gallery
// @Description  API for creating Gallery
// @Tags         Galleries
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param 	     payload body request.GalleriesCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /galleries [post]
func (_i *galleriesController) Save(c *fiber.Ctx) error {
	req := new(request.GalleriesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	dataResult, err := _i.galleriesService.Create(*req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Gallery successfully created"},
		Data:     dataResult,
	})
}

// Update Gallery
// @Summary      Update Gallery
// @Description  API for updating Gallery
// @Tags         Galleries
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        id path int true "Gallery ID"
// @Param 	     payload body request.GalleriesUpdateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /galleries/{id} [put]
func (_i *galleriesController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

	dataResult, err := _i.galleriesService.Update(uint(id), *req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Gallery successfully updated"},
		Data:     dataResult,
	})
}

// Delete Gallery
// @Summary      Delete Gallery
// @Description  API for deleting Gallery (soft delete)
// @Tags         Galleries
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        id path int true "Gallery ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /galleries/{id} [delete]
func (_i *galleriesController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Gallery successfully deleted"},
	})
}
