package controller

import (
	"strconv"
	"strings"

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

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

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

type ourServiceContentImagesController struct {
	service service.OurServiceContentImagesService
	Log     zerolog.Logger
}

type OurServiceContentImagesController interface {
	FindByOurServiceContentID(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewOurServiceContentImagesController(service service.OurServiceContentImagesService, log zerolog.Logger) OurServiceContentImagesController {
	return &ourServiceContentImagesController{
		service: service,
		Log:     log,
	}
}

func (_i *ourServiceContentImagesController) FindByOurServiceContentID(c *fiber.Ctx) error {
	contentIDStr := c.Params("our_service_content_id")

	contentIDInt, err := strconv.Atoi(contentIDStr)
	if err != nil {
		return err
	}

	contentID := uint(contentIDInt)

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

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

func (_i *ourServiceContentImagesController) Save(c *fiber.Ctx) error {
	if strings.HasPrefix(c.Get("Content-Type"), "multipart/form-data") {
		cidStr := c.FormValue("our_service_content_id")
		cidInt, err := strconv.Atoi(cidStr)
		if err != nil {
			return err
		}
		cid := uint(cidInt)
		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 service content image (upload)")
			return err
		}
		return utilRes.Resp(c, utilRes.Response{
			Success:  true,
			Messages: utilRes.Messages{"Our service content image created"},
			Data:     result,
		})
	}

	req := new(request.OurServiceContentImageCreateRequest)

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

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

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

	idInt, err := strconv.Atoi(idStr)
	if err != nil {
		return err
	}

	id := uint(idInt)

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

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

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

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

	idInt, err := strconv.Atoi(idStr)
	if err != nil {
		return err
	}

	id := uint(idInt)

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

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