package controller

import (
	"netidhub-saas-be/app/middleware"
	"netidhub-saas-be/app/module/feedbacks/request"
	"netidhub-saas-be/app/module/feedbacks/service"
	"netidhub-saas-be/utils/paginator"
	utilRes "netidhub-saas-be/utils/response"
	utilVal "netidhub-saas-be/utils/validator"
	"strconv"

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

type feedbacksController struct {
	feedbacksService service.FeedbacksService
	Log              zerolog.Logger
}

type FeedbacksController 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
	FeedbackMonthlyStats(c *fiber.Ctx) error
}

func NewFeedbacksController(feedbacksService service.FeedbacksService, log zerolog.Logger) FeedbacksController {
	return &feedbacksController{
		feedbacksService: feedbacksService,
		Log:              log,
	}
}

// All get all Feedbacks
// @Summary      Get all Feedbacks
// @Description  API for getting all Feedbacks
// @Tags         Feedbacks
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param 	     req query request.FeedbacksQueryRequest false "query parameters"
// @Param 	     req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /feedbacks [get]
func (_i *feedbacksController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.FeedbacksQueryRequestContext{
		Message:          c.Query("message"),
		CommentFromName:  c.Query("commentFromName"),
		CommentFromEmail: c.Query("commentFromEmail"),
		StatusId:         c.Query("statusId"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	feedbacksData, paging, err := _i.feedbacksService.All(clientId, req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Feedbacks list successfully retrieved"},
		Data:     feedbacksData,
		Meta:     paging,
	})
}

// Show get one Feedbacks
// @Summary      Get one Feedbacks
// @Description  API for getting one Feedbacks
// @Tags         Feedbacks
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        id path int true "Feedbacks ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /feedbacks/{id} [get]
func (_i *feedbacksController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	feedbacksData, err := _i.feedbacksService.Show(clientId, uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Feedbacks successfully retrieved"},
		Data:     feedbacksData,
	})
}

// Save create Feedbacks
// @Summary      Create Feedbacks
// @Description  API for create Feedbacks
// @Tags         Feedbacks
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param 	     payload body request.FeedbacksCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /feedbacks [post]
func (_i *feedbacksController) Save(c *fiber.Ctx) error {
	req := new(request.FeedbacksCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	authToken := c.Get("Authorization")

	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	dataResult, err := _i.feedbacksService.Save(clientId, *req, authToken)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Feedbacks successfully created"},
		Data:     dataResult,
	})
}

// Update update Feedbacks
// @Summary      update Feedbacks
// @Description  API for update Feedbacks
// @Tags         Feedbacks
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param 	     payload body request.FeedbacksUpdateRequest true "Required payload"
// @Param        id path int true "Feedbacks ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /feedbacks/{id} [put]
func (_i *feedbacksController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	err = _i.feedbacksService.Update(clientId, uint(id), *req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Feedbacks successfully updated"},
	})
}

// Delete delete Feedbacks
// @Summary      delete Feedbacks
// @Description  API for delete Feedbacks
// @Tags         Feedbacks
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param        id path int true "Feedbacks ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /feedbacks/{id} [delete]
func (_i *feedbacksController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	err = _i.feedbacksService.Delete(clientId, uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Feedbacks successfully deleted"},
	})
}

// FeedbackMonthlyStats Feedbacks
// @Summary      FeedbackMonthlyStats Feedbacks
// @Description  API for FeedbackMonthlyStats of Feedbacks
// @Tags         Feedbacks
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param 	     year query int false "year"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /feedbacks/statistic/monthly [get]
func (_i *feedbacksController) FeedbackMonthlyStats(c *fiber.Ctx) error {
	authToken := c.Get("Authorization")
	year := c.Query("year")
	yearInt, err := strconv.Atoi(year)
	if err != nil {
		return err
	}

	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	response, err := _i.feedbacksService.FeedbackMonthlyStats(clientId, authToken, &yearInt)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"FeedbacksMonthlyStats of Feedbacks successfully retrieved"},
		Data:     response,
	})
}
