package controller

import (
	"strings"

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

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

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

type heroContentImagesController struct {
	service service.HeroContentImagesService
	Log     zerolog.Logger
}

type HeroContentImagesController interface {
	FindByHeroID(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewHeroContentImagesController(service service.HeroContentImagesService, log zerolog.Logger) HeroContentImagesController {
	return &heroContentImagesController{
		service: service,
		Log:     log,
	}
}

func (_i *heroContentImagesController) FindByHeroID(c *fiber.Ctx) error {
	heroIDStr := c.Params("hero_id")

	heroID, err := uuid.Parse(heroIDStr)
	if err != nil {
		return err
	}

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

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

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

	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 hero content image")
		return err
	}

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

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

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

	if strings.HasPrefix(c.Get("Content-Type"), "multipart/form-data") {
		file, err := c.FormFile("file")
		if err != nil {
			return err
		}
		if err := _i.service.UpdateWithFile(id, file); err != nil {
			_i.Log.Error().Err(err).Msg("failed update hero content image (upload)")
			return err
		}
		return utilRes.Resp(c, utilRes.Response{
			Success:  true,
			Messages: utilRes.Messages{"Hero content image updated"},
		})
	}

	req := new(request.HeroContentImageUpdateRequest)
	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 hero content image")
		return err
	}

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

func (_i *heroContentImagesController) 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 hero content image")
		return err
	}

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