package controller

import (
	"campaign-pool-be/app/module/campaigns/request"
	"campaign-pool-be/app/module/campaigns/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 campaignsController struct {
	campaignsService service.CampaignsService
	Log              zerolog.Logger
}

type CampaignsController 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 NewCampaignsController(campaignsService service.CampaignsService, log zerolog.Logger) CampaignsController {
	return &campaignsController{
		campaignsService: campaignsService,
		Log:              log,
	}
}

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

	reqContext := request.CampaignsQueryRequestContext{
		Title:           c.Query("title"),
		CampaignTypeID:  c.Query("campaignTypeId"),
		Status:          c.Query("status"),
		CreatorID:       c.Query("creatorId"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

// Save Campaigns
// @Summary      Create Campaigns
// @Description  API for create Campaigns
// @Tags         Campaigns
// @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.CampaignsCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /campaigns [post]
func (_i *campaignsController) Save(c *fiber.Ctx) error {
	req := new(request.CampaignsCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	authToken := c.Get("Authorization")

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

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

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

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

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

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

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

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

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

