package controller

import (
	"netidhub-saas-be/app/module/magazine_files/request"
	"netidhub-saas-be/app/module/magazine_files/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 magazineFilesController struct {
	magazineFilesService service.MagazineFilesService
}

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

func NewMagazineFilesController(magazineFilesService service.MagazineFilesService) MagazineFilesController {
	return &magazineFilesController{
		magazineFilesService: magazineFilesService,
	}
}

// All MagazineFiles
// @Summary      Get all MagazineFiles
// @Description  API for getting all MagazineFiles
// @Tags         Magazine Files
// @Security     Bearer
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /magazine-files [get]
func (_i *magazineFilesController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

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

	magazineFilesData, paging, err := _i.magazineFilesService.All(req)
	if err != nil {
		return err
	}

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

// Show MagazineFiles
// @Summary      Get one MagazineFiles
// @Description  API for getting one MagazineFiles
// @Tags         Magazine Files
// @Security     Bearer
// @Param        id path int true "MagazineFiles ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /magazine-files/{id} [get]
func (_i *magazineFilesController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	magazineFilesData, err := _i.magazineFilesService.Show(uint(id))
	if err != nil {
		return err
	}

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

// Save MagazineFiles
// @Summary      Create MagazineFiles
// @Description  API for create MagazineFiles
// @Tags         Magazine Files
// @Security     Bearer
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param        files formData file true "Upload file" multiple true
// @Param        title formData string true "Magazine file title"
// @Param        description formData string true "Magazine file description"
// @Param        magazineId path int true "Magazine ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /magazine-files/{magazineId} [post]
func (_i *magazineFilesController) Save(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("magazineId"), 10, 0)
	if err != nil {
		return err
	}
	title := c.Params("title")
	description := c.Params("description")

	err = _i.magazineFilesService.Save(c, uint(id), title, description)
	if err != nil {
		return err
	}

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

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

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

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

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

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

	err = _i.magazineFilesService.Delete(uint(id))
	if err != nil {
		return err
	}

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

// Viewer MagazineFiles
// @Summary      Create MagazineFiles
// @Description  API for create MagazineFiles
// @Tags         Magazine Files
// @Security     Bearer
// @Param        filename path string true "Magazine File Name"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /magazine-files/viewer/{filename} [get]
func (_i *magazineFilesController) Viewer(c *fiber.Ctx) error {
	return _i.magazineFilesService.Viewer(c)
}
