package controller

import (
	"strconv"

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

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

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

type aboutUsContentImageController struct {
	service service.AboutUsContentImageService
	Log     zerolog.Logger
}

type AboutUsContentImageController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	SaveRemote(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewAboutUsContentImageController(service service.AboutUsContentImageService, log zerolog.Logger) AboutUsContentImageController {
	return &aboutUsContentImageController{
		service: service,
		Log:     log,
	}
}

// GET ALL
func (_i *aboutUsContentImageController) All(c *fiber.Ctx) error {
	_i.Log.Info().Msg("GET /about-us-content-images")

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

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

// GET BY ID
func (_i *aboutUsContentImageController) Show(c *fiber.Ctx) error {
	id, err := strconv.Atoi(c.Params("id"))
	if err != nil {
		return err
	}

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

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

// CREATE
func (_i *aboutUsContentImageController) Save(c *fiber.Ctx) error {
	// Ambil about_us_content_id dari form
	aboutUsContentId, err := strconv.Atoi(c.FormValue("about_us_content_id"))
	if err != nil {
		return err
	}

	// Ambil file
	file, err := c.FormFile("file")
	if err != nil {
		return err
	}

	// Kirim ke service
	result, err := _i.service.Save(uint(aboutUsContentId), file)
	if err != nil {
		_i.Log.Error().Err(err).Msg("failed create about us content image")
		return err
	}

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

// SaveRemote JSON: public URL for image or video (e.g. CDN .mp4)
func (_i *aboutUsContentImageController) SaveRemote(c *fiber.Ctx) error {
	req := new(request.AboutUsContentImageRemoteRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}
	result, err := _i.service.SaveRemoteURL(req.AboutUsContentID, req.MediaURL, req.MediaType)
	if err != nil {
		_i.Log.Error().Err(err).Msg("failed save remote about us media")
		return err
	}
	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"About us media URL saved"},
		Data:     result,
	})
}

// DELETE
func (_i *aboutUsContentImageController) Delete(c *fiber.Ctx) error {
	id, err := strconv.Atoi(c.Params("id"))
	if err != nil {
		return err
	}

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

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