package controller

import (
	"strconv"
	"web-qudo-be/app/middleware"
	"web-qudo-be/app/module/subscription/request"
	"web-qudo-be/app/module/subscription/service"
	"web-qudo-be/utils/paginator"
	utilRes "web-qudo-be/utils/response"
	utilVal "web-qudo-be/utils/validator"

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

type subscriptionController struct {
	subscriptionService service.SubscriptionService
	Log                 zerolog.Logger
}

type SubscriptionController 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 NewSubscriptionController(subscriptionService service.SubscriptionService, log zerolog.Logger) SubscriptionController {
	return &subscriptionController{
		subscriptionService: subscriptionService,
		Log:                 log,
	}
}

// All get all Subscription
// @Summary      Get all Subscription
// @Description  API for getting all Subscription
// @Tags         Subscription
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param 	     req query request.SubscriptionQueryRequest 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       /subscription [get]
func (_i *subscriptionController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.SubscriptionQueryRequestContext{
		Email: c.Query("email"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

// Show get one Subscription
// @Summary      Get one Subscription
// @Description  API for getting one Subscription
// @Tags         Subscription
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        id path int true "Subscription ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /subscription/{id} [get]
func (_i *subscriptionController) 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)

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

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

// Save create Subscription
// @Summary      Create Subscription
// @Description  API for create Subscription
// @Tags         Subscription
// @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.SubscriptionCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /subscription [post]
func (_i *subscriptionController) Save(c *fiber.Ctx) error {
	req := new(request.SubscriptionCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

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

// Update update Subscription
// @Summary      update Subscription
// @Description  API for update Subscription
// @Tags         Subscription
// @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.SubscriptionUpdateRequest true "Required payload"
// @Param        id path int true "Subscription ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /subscription/{id} [put]
func (_i *subscriptionController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

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

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

// Delete delete Subscription
// @Summary      delete Subscription
// @Description  API for delete Subscription
// @Tags         Subscription
// @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 "Subscription ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /subscription/{id} [delete]
func (_i *subscriptionController) 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.subscriptionService.Delete(clientId, uint(id))
	if err != nil {
		return err
	}

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