package controller

import (
	"campaign-pool-be/app/module/campaign_types/request"
	"campaign-pool-be/app/module/campaign_types/service"
	"campaign-pool-be/utils/paginator"
	"strconv"

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

	utilRes "campaign-pool-be/utils/response"
	utilVal "campaign-pool-be/utils/validator"
)

type campaignTypesController struct {
	campaignTypesService service.CampaignTypesService
	Log                  zerolog.Logger
}

type CampaignTypesController 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 NewCampaignTypesController(campaignTypesService service.CampaignTypesService, log zerolog.Logger) CampaignTypesController {
	return &campaignTypesController{
		campaignTypesService: campaignTypesService,
		Log:                  log,
	}
}

// All CampaignTypes
// @Summary      Get all CampaignTypes
// @Description  API for getting all CampaignTypes
// @Tags         CampaignTypes
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param 	     req query request.CampaignTypesQueryRequest 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       /campaign-types [get]
func (_i *campaignTypesController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.CampaignTypesQueryRequestContext{
		Name: c.Query("name"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

	authToken := c.Get("Authorization")
	campaignTypesData, paging, err := _i.campaignTypesService.All(req, authToken)
	if err != nil {
		return err
	}

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

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

	campaignTypesData, err := _i.campaignTypesService.Show(uint(id))
	if err != nil {
		return err
	}

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

// Save CampaignTypes
// @Summary      Create CampaignTypes
// @Description  API for create CampaignTypes
// @Tags         CampaignTypes
// @Security     Bearer
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param 	     payload body request.CampaignTypesCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /campaign-types [post]
func (_i *campaignTypesController) Save(c *fiber.Ctx) error {
	req := new(request.CampaignTypesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	authToken := c.Get("Authorization")

	dataResult, err := _i.campaignTypesService.Save(*req, authToken)
	if err != nil {
		return err
	}

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

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

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

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

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

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

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

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

