package controller

import (
	"campaign-pool-be/app/module/campaign_files/request"
	"campaign-pool-be/app/module/campaign_files/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 campaignFilesController struct {
	campaignFilesService service.CampaignFilesService
	Log                  zerolog.Logger
}

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

func NewCampaignFilesController(campaignFilesService service.CampaignFilesService, log zerolog.Logger) CampaignFilesController {
	return &campaignFilesController{
		campaignFilesService: campaignFilesService,
		Log:                  log,
	}
}

func (_i *campaignFilesController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.CampaignFilesQueryRequestContext{
		CampaignID: c.Query("campaignId"),
		Type:       c.Query("type"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

func (_i *campaignFilesController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

func (_i *campaignFilesController) GetByCampaign(c *fiber.Ctx) error {
	campaignID, err := strconv.ParseUint(c.Params("campaignId"), 10, 0)
	if err != nil {
		return err
	}

	data, err := _i.campaignFilesService.GetByCampaign(uint(campaignID))
	if err != nil {
		return err
	}

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

func (_i *campaignFilesController) Save(c *fiber.Ctx) error {
	req := new(request.CampaignFilesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

func (_i *campaignFilesController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

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

func (_i *campaignFilesController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

