package controller

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

	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog"

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

type customStaticPagesController struct {
	customStaticPagesService service.CustomStaticPagesService
	Log                      zerolog.Logger
}

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

func NewCustomStaticPagesController(customStaticPagesService service.CustomStaticPagesService, log zerolog.Logger) CustomStaticPagesController {
	return &customStaticPagesController{
		customStaticPagesService: customStaticPagesService,
		Log:                      log,
	}
}

// All get all CustomStaticPages
// @Summary      Get all CustomStaticPages
// @Description  API for getting all CustomStaticPages
// @Tags         CustomStaticPages
// @Security     Bearer
// @Param 	     req query request.CustomStaticPagesQueryRequest 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       /custom-static-pages [get]
func (_i *customStaticPagesController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.CustomStaticPagesQueryRequestContext{
		Title:       c.Query("title"),
		Description: c.Query("description"),
		Slug:        c.Query("slug"),
		HtmlBody:    c.Query("htmlBody"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

	// Get from context
	customStaticPagesData, paging, err := _i.customStaticPagesService.All(req)
	if err != nil {
		return err
	}

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

// Show get one CustomStaticPages
// @Summary      Get one CustomStaticPages
// @Description  API for getting one CustomStaticPages
// @Tags         CustomStaticPages
// @Security     Bearer
// @Param        id path int true "CustomStaticPages ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /custom-static-pages/{id} [get]
func (_i *customStaticPagesController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	// Get from context
	customStaticPagesData, err := _i.customStaticPagesService.Show(uint(id))
	if err != nil {
		return err
	}

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

// ShowBySlug get one CustomStaticPages
// @Summary      Get one CustomStaticPages
// @Description  API for getting one CustomStaticPages
// @Tags         CustomStaticPages
// @Security     Bearer
// @Param        slug path string true "CustomStaticPages Slug"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /custom-static-pages/slug/{slug} [get]
func (_i *customStaticPagesController) ShowBySlug(c *fiber.Ctx) error {
	slug := c.Params("slug")

	// Get from context
	customStaticPagesData, err := _i.customStaticPagesService.ShowBySlug(slug)
	if err != nil {
		return err
	}

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

// Save create CustomStaticPages
// @Summary      Create CustomStaticPages
// @Description  API for create CustomStaticPages
// @Tags         CustomStaticPages
// @Security     Bearer
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param 	     payload body request.CustomStaticPagesCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /custom-static-pages [post]
func (_i *customStaticPagesController) Save(c *fiber.Ctx) error {
	req := new(request.CustomStaticPagesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	// Get from context
	authToken := c.Get("Authorization")
	dataResult, err := _i.customStaticPagesService.Save(*req, authToken)
	if err != nil {
		return err
	}

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

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

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

	// Get from context
	err = _i.customStaticPagesService.Update(uint(id), *req)
	if err != nil {
		return err
	}

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

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

	// Get from context
	err = _i.customStaticPagesService.Delete(uint(id))
	if err != nil {
		return err
	}

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