package controller

import (
	"netidhub-saas-be/app/middleware"
	"netidhub-saas-be/app/module/magazines/request"
	"netidhub-saas-be/app/module/magazines/service"
	"netidhub-saas-be/utils/paginator"
	"strconv"

	"github.com/gofiber/fiber/v2"

	utilRes "netidhub-saas-be/utils/response"
	utilVal "netidhub-saas-be/utils/validator"
)

type magazinesController struct {
	magazinesService service.MagazinesService
}

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

func NewMagazinesController(magazinesService service.MagazinesService) MagazinesController {
	return &magazinesController{
		magazinesService: magazinesService,
	}
}

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

	reqContext := request.MagazinesQueryRequestContext{
		Title:       c.Query("title"),
		Description: c.Query("description"),
		CreatedById: c.Query("createdById"),
		StatusId:    c.Query("statusId"),
		IsPublish:   c.Query("isPublish"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

	return utilRes.Resp(c, utilRes.Response{
		Messages: utilRes.Messages{"Magazines list successfully retrieved"},
		Data:     magazinesData,
		Meta:     paging,
	})
}

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

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

	return utilRes.Resp(c, utilRes.Response{
		Messages: utilRes.Messages{"Magazines successfully retrieved"},
		Data:     magazinesData,
	})
}

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

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

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

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

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

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

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

// SaveThumbnail Magazines
// @Summary      Save Thumbnail Magazines
// @Description  API for Save Thumbnail of Magazines
// @Tags         Magazines
// @Security     Bearer
// @Produce      json
// @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 "Magazine ID"
// @Param        files formData file true "Upload thumbnail"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /magazines/thumbnail/{id} [post]
func (_i *magazinesController) SaveThumbnail(c *fiber.Ctx) error {
	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	err := _i.magazinesService.SaveThumbnail(clientId, c)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Thumbnail of Magazines successfully created"},
	})
}

// Viewer Magazines
// @Summary      Viewer Magazines Thumbnail
// @Description  API for View Thumbnail of Magazines
// @Tags         Magazines
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        thumbnailName path string true "Magazines Thumbnail Name"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /magazines/thumbnail/viewer/{thumbnailName} [get]
func (_i *magazinesController) Viewer(c *fiber.Ctx) error {
	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	return _i.magazinesService.Viewer(clientId, c)
}

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

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