package controller

import (
	"campaign-pool-be/app/module/advertisement/request"
	"campaign-pool-be/app/module/advertisement/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 advertisementController struct {
	advertisementService service.AdvertisementService
	Log                  zerolog.Logger
}

type AdvertisementController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Upload(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	UpdatePublish(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
	Viewer(c *fiber.Ctx) error
}

func NewAdvertisementController(advertisementService service.AdvertisementService, log zerolog.Logger) AdvertisementController {
	return &advertisementController{
		advertisementService: advertisementService,
		Log:                  log,
	}
}

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

	reqContext := request.AdvertisementQueryRequestContext{
		Title:        c.Query("title"),
		Description:  c.Query("description"),
		RedirectLink: c.Query("redirectLink"),
		Placement:    c.Query("placement"),
		StatusId:     c.Query("statusId"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

	advertisementData, paging, err := _i.advertisementService.All(req)
	if err != nil {
		return err
	}

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

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

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

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

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

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

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

// Upload Advertisement
// @Summary      Upload Advertisement
// @Description  API for Upload File Advertisement
// @Tags         Advertisement
// @Security     Bearer
// @Produce      json
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        file formData file true "Upload file" multiple false
// @Param        id path int true "Advertisement ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /advertisement/upload/{id} [post]
func (_i *advertisementController) Upload(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	err = _i.advertisementService.Upload(c, uint(id))
	if err != nil {
		return err
	}

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

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

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

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

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

// UpdatePublish Advertisement
// @Summary      Update Publish Advertisement
// @Description  API for Update Publish Advertisement
// @Tags         Advertisement
// @Security     Bearer
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param        id path int true "Advertisement ID"
// @Param        isPublish query bool true "Advertisement Publish Status"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /advertisement/publish/{id} [put]
func (_i *advertisementController) UpdatePublish(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	isPublish, err := strconv.ParseBool(c.Query("isPublish"))
	if err != nil {
		return err
	}

	err = _i.advertisementService.UpdatePublish(uint(id), isPublish)
	if err != nil {
		return err
	}

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

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

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

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

// Viewer Advertisement
// @Summary      Viewer Advertisement
// @Description  API for Viewer Advertisement
// @Tags         Advertisement
// @Security     Bearer
// @Param        filename path string true "Content File Name"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /advertisement/viewer/{filename} [get]
func (_i *advertisementController) Viewer(c *fiber.Ctx) error {
	return _i.advertisementService.Viewer(c)
}
