package controller

import (
	"strings"

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

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

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

type ourProductContentImagesController struct {
	service service.OurProductContentImagesService
	Log     zerolog.Logger
}

type OurProductContentImagesController interface {
	FindByOurProductContentID(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewOurProductContentImagesController(service service.OurProductContentImagesService, log zerolog.Logger) OurProductContentImagesController {
	return &ourProductContentImagesController{
		service: service,
		Log:     log,
	}
}

func (_i *ourProductContentImagesController) FindByOurProductContentID(c *fiber.Ctx) error {
	contentIDStr := c.Params("content_id")

	contentID, err := uuid.Parse(contentIDStr)
	if err != nil {
		return err
	}

	data, err := _i.service.FindByContentID(contentID)
	if err != nil {
		_i.Log.Error().Err(err).Msg("failed get our product content image")
		return err
	}

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

func (_i *ourProductContentImagesController) Save(c *fiber.Ctx) error {
	if strings.HasPrefix(c.Get("Content-Type"), "multipart/form-data") {
		cidStr := c.FormValue("our_product_content_id")
		cid, err := uuid.Parse(cidStr)
		if err != nil {
			return err
		}
		file, err := c.FormFile("file")
		if err != nil {
			return err
		}
		result, err := _i.service.SaveWithFile(cid, file)
		if err != nil {
			_i.Log.Error().Err(err).Msg("failed create our product content image (upload)")
			return err
		}
		return utilRes.Resp(c, utilRes.Response{
			Success:  true,
			Messages: utilRes.Messages{"Our product content image created"},
			Data:     result,
		})
	}

	req := new(request.OurProductContentImageCreateRequest)

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

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

func (_i *ourProductContentImagesController) 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 our product content image (upload)")
			return err
		}
		return utilRes.Resp(c, utilRes.Response{
			Success:  true,
			Messages: utilRes.Messages{"Our product content image updated"},
		})
	}

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

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

func (_i *ourProductContentImagesController) 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 our product content image")
		return err
	}

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