package controller

import (
	"github.com/gofiber/fiber/v2"
	"go-humas-be/app/module/ppid_data_approval_histories/request"
	"go-humas-be/app/module/ppid_data_approval_histories/service"
	"strconv"

	utilRes "go-humas-be/utils/response"
	utilVal "go-humas-be/utils/validator"
)

type ppidDataApprovalHistoriesController struct {
	ppidDataApprovalHistoriesService service.PpidDataApprovalHistoriesService
}

type PpidDataApprovalHistoriesController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	ShowByPpidData(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewPpidDataApprovalHistoriesController(ppidDataApprovalHistoriesService service.PpidDataApprovalHistoriesService) PpidDataApprovalHistoriesController {
	return &ppidDataApprovalHistoriesController{
		ppidDataApprovalHistoriesService: ppidDataApprovalHistoriesService,
	}
}

// All PpidDataApprovalHistories
// @Summary      Get all PpidDataApprovalHistories
// @Description  API for getting all PpidDataApprovalHistories
// @Tags         Task
// @Security     Bearer
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ppid-data-approval-histories [get]
func (_i *ppidDataApprovalHistoriesController) All(c *fiber.Ctx) error {
	ppidDataApprovalHistoriesData, err := _i.ppidDataApprovalHistoriesService.All()
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"PpidDataApprovalHistories list successfully retrieved"},
		Data:     ppidDataApprovalHistoriesData,
	})
}

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

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

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

// ShowByPpidData PpidDataApprovalHistories
// @Summary      Get one PpidDataApprovalHistories
// @Description  API for getting one PpidDataApprovalHistories
// @Tags         Task
// @Security     Bearer
// @Param        ppidDataId path int true "PpidData ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ppid-data-approval-histories/ppid-data/:ppidDataId [get]
func (_i *ppidDataApprovalHistoriesController) ShowByPpidData(c *fiber.Ctx) error {
	ppidDataId, err := strconv.Atoi(c.Params("ppidDataId"))
	if err != nil {
		return err
	}

	ppidDataApprovalHistoriesData, err := _i.ppidDataApprovalHistoriesService.ShowByPpidData(uint(ppidDataId))
	if err != nil {
		return err
	}

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

// Save create PpidDataApprovalHistories
// @Summary      Create PpidDataApprovalHistories
// @Description  API for create PpidDataApprovalHistories
// @Tags         Task
// @Security     Bearer
// @Param        Authorization header string true "Insert your access token" default(Bearer <Add access token here>)
// @Body 	     request.PpidDataApprovalHistoriesCreateRequest
// @Success      200  {object}  response.Response
// @Failure      401  {object}  response.Response
// @Failure      404  {object}  response.Response
// @Failure      422  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /ppid-data-approval-histories [post]
func (_i *ppidDataApprovalHistoriesController) Save(c *fiber.Ctx) error {
	req := new(request.PpidDataApprovalHistoriesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	authToken := c.Get("Authorization")

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

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

// Delete delete PpidDataApprovalHistories
// @Summary      delete PpidDataApprovalHistories
// @Description  API for delete PpidDataApprovalHistories
// @Tags         Task
// @Security     Bearer
// @Param        id path int true "PpidDataApprovalHistories ID"
// @Success      200  {object}  response.Response
// @Failure      401  {object}  response.Response
// @Failure      404  {object}  response.Response
// @Failure      422  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /ppid-data-approval-histories/:id [delete]
func (_i *ppidDataApprovalHistoriesController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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