package controller

import (
	"campaign-pool-be/app/module/campaign_destinations/request"
	"campaign-pool-be/app/module/campaign_destinations/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 campaignDestinationsController struct {
	campaignDestinationsService service.CampaignDestinationsService
	Log                         zerolog.Logger
}

type CampaignDestinationsController 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 NewCampaignDestinationsController(campaignDestinationsService service.CampaignDestinationsService, log zerolog.Logger) CampaignDestinationsController {
	return &campaignDestinationsController{
		campaignDestinationsService: campaignDestinationsService,
		Log:                         log,
	}
}

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

	reqContext := request.CampaignDestinationsQueryRequestContext{
		Name:            c.Query("name"),
		CampaignTypeID: c.Query("campaignTypeId"),
		IsActive:        c.Query("isActive"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

// Save CampaignDestinations
// @Summary      Create CampaignDestinations
// @Description  API for create CampaignDestinations
// @Tags         CampaignDestinations
// @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.CampaignDestinationsCreateRequest 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-destinations [post]
func (_i *campaignDestinationsController) Save(c *fiber.Ctx) error {
	req := new(request.CampaignDestinationsCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	authToken := c.Get("Authorization")

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

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

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

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

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

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

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

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

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

