package controller

import (
	"github.com/gofiber/fiber/v2"
	"github.com/google/uuid"
	"github.com/rs/zerolog"

	"web-qudo-be/app/module/partner_contents/request"
	"web-qudo-be/app/module/partner_contents/service"

	utilRes "web-qudo-be/utils/response"
	utilVal "web-qudo-be/utils/validator"
)

type partnerContentController struct {
	service service.PartnerContentService
	Log     zerolog.Logger
}

type PartnerContentController interface {
	Show(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	UploadLogo(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewPartnerContentController(service service.PartnerContentService, log zerolog.Logger) PartnerContentController {
	return &partnerContentController{
		service: service,
		Log:     log,
	}
}

func (_i *partnerContentController) Show(c *fiber.Ctx) error {
	_i.Log.Info().Msg("GET /partner-contents")

	data, err := _i.service.Show()
	if err != nil {
		_i.Log.Error().Err(err).Msg("failed get partner content")
		return err
	}

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

func (_i *partnerContentController) Save(c *fiber.Ctx) error {
	req := new(request.PartnerContentCreateRequest)

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

	data := req.ToEntity()

	result, err := _i.service.Save(data)
	if err != nil {
		_i.Log.Error().Err(err).Msg("failed create partner content")
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Partner content created"},
		Data:     result,
	})
}

func (_i *partnerContentController) Update(c *fiber.Ctx) error {
	idStr := c.Params("id")

	id, err := uuid.Parse(idStr)
	if err != nil {
		return err
	}

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

	err = _i.service.Update(id, req.ToEntity())
	if err != nil {
		_i.Log.Error().Err(err).Msg("failed update partner content")
		return err
	}

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

func (_i *partnerContentController) UploadLogo(c *fiber.Ctx) error {
	idStr := c.Params("id")
	id, err := uuid.Parse(idStr)
	if err != nil {
		return err
	}
	file, err := c.FormFile("file")
	if err != nil {
		return err
	}
	if err := _i.service.UploadLogo(id, file); err != nil {
		_i.Log.Error().Err(err).Msg("failed upload partner logo")
		return err
	}
	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Partner logo uploaded"},
	})
}

func (_i *partnerContentController) Delete(c *fiber.Ctx) error {
	idStr := c.Params("id")

	id, err := uuid.Parse(idStr)
	if err != nil {
		return err
	}

	err = _i.service.Delete(id)
	if err != nil {
		_i.Log.Error().Err(err).Msg("failed delete partner content")
		return err
	}

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