package controller

import (
	"strconv"

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

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

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

type aboutUsContentController struct {
	service service.AboutUsContentService
	Log     zerolog.Logger
}

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

func NewAboutUsContentController(service service.AboutUsContentService, log zerolog.Logger) AboutUsContentController {
	return &aboutUsContentController{
		service: service,
		Log:     log,
	}
}

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

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

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

// GET BY ID
func (_i *aboutUsContentController) 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")
		return err
	}

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

// CREATE
func (_i *aboutUsContentController) Save(c *fiber.Ctx) error {
	req := new(request.AboutUsContentCreateRequest)

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

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

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

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

	req := new(request.AboutUsContentUpdateRequest)

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

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

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

// DELETE (soft delete)
func (_i *aboutUsContentController) 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")
		return err
	}

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