package controller

import (
	"web-qudo-be/app/middleware"
	"web-qudo-be/app/module/cms_content_submissions/request"
	"web-qudo-be/app/module/cms_content_submissions/service"
	"web-qudo-be/utils/paginator"

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

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

type CmsContentSubmissionsController interface {
	Submit(c *fiber.Ctx) error
	List(c *fiber.Ctx) error
	Approve(c *fiber.Ctx) error
	Reject(c *fiber.Ctx) error
}

type cmsContentSubmissionsController struct {
	svc service.CmsContentSubmissionsService
}

func NewCmsContentSubmissionsController(svc service.CmsContentSubmissionsService) CmsContentSubmissionsController {
	return &cmsContentSubmissionsController{svc: svc}
}

func (_i *cmsContentSubmissionsController) Submit(c *fiber.Ctx) error {
	user := middleware.GetUser(c)
	clientID := middleware.GetClientID(c)
	if user == nil || clientID == nil {
		return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized")
	}
	req := new(request.SubmitCmsContentSubmissionRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}
	row, err := _i.svc.Submit(clientID, user, req)
	if err != nil {
		return fiber.NewError(fiber.StatusBadRequest, err.Error())
	}
	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"CMS submission saved"},
		Data:     row,
	})
}

func (_i *cmsContentSubmissionsController) List(c *fiber.Ctx) error {
	user := middleware.GetUser(c)
	clientID := middleware.GetClientID(c)
	if user == nil || clientID == nil {
		return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized")
	}
	p, err := paginator.Paginate(c)
	if err != nil {
		return err
	}
	status := c.Query("status")
	mineOnly := c.Query("mine") == "1" || c.Query("mine") == "true"
	rows, paging, err := _i.svc.List(clientID, user, status, mineOnly, p)
	if err != nil {
		return fiber.NewError(fiber.StatusBadRequest, err.Error())
	}
	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"CMS submissions loaded"},
		Data:     rows,
		Meta:     paging,
	})
}

func (_i *cmsContentSubmissionsController) Approve(c *fiber.Ctx) error {
	user := middleware.GetUser(c)
	clientID := middleware.GetClientID(c)
	if user == nil || clientID == nil {
		return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized")
	}
	id, err := uuid.Parse(c.Params("id"))
	if err != nil {
		return err
	}
	if err := _i.svc.Approve(clientID, user, id); err != nil {
		return fiber.NewError(fiber.StatusBadRequest, err.Error())
	}
	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"CMS submission approved and applied"},
	})
}

func (_i *cmsContentSubmissionsController) Reject(c *fiber.Ctx) error {
	user := middleware.GetUser(c)
	clientID := middleware.GetClientID(c)
	if user == nil || clientID == nil {
		return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized")
	}
	id, err := uuid.Parse(c.Params("id"))
	if err != nil {
		return err
	}
	req := new(request.RejectCmsContentSubmissionRequest)
	_ = c.BodyParser(req)
	if err := _i.svc.Reject(clientID, user, id, req.Note); err != nil {
		return fiber.NewError(fiber.StatusBadRequest, err.Error())
	}
	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"CMS submission rejected"},
	})
}
