package controller

import (
	"github.com/gofiber/fiber/v2"
	"strconv"
	"web-qudo-be/app/module/provinces/request"
	"web-qudo-be/app/module/provinces/service"
	"web-qudo-be/utils/paginator"

	utilRes "web-qudo-be/utils/response"
	utilVal "web-qudo-be/utils/validator"
)

type provincesController struct {
	provincesService service.ProvincesService
}

type ProvincesController 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 NewProvincesController(provincesService service.ProvincesService) ProvincesController {
	return &provincesController{
		provincesService: provincesService,
	}
}

// All Provinces
// @Summary      Get all Provinces
// @Description  API for getting all Provinces
// @Tags         Untags
// @Security     Bearer
// @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       /provinces [get]
func (_i *provincesController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	var req request.ProvincesQueryRequest
	req.Pagination = paginate

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

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

// Show Provinces
// @Summary      Get one Provinces
// @Description  API for getting one Provinces
// @Tags         Untags
// @Security     Bearer
// @Param        id path int true "Provinces 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       /provinces/{id} [get]
func (_i *provincesController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

// Save Provinces
// @Summary      Create Provinces
// @Description  API for create Provinces
// @Tags         Untags
// @Security     Bearer
// @Body 	     request.ProvincesCreateRequest
// @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       /provinces [post]
func (_i *provincesController) Save(c *fiber.Ctx) error {
	req := new(request.ProvincesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

// Update Provinces
// @Summary      Update Provinces
// @Description  API for update Provinces
// @Tags         Untags
// @Security     Bearer
// @Body 	     request.ProvincesUpdateRequest
// @Param        id path int true "Provinces 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       /provinces/{id} [put]
func (_i *provincesController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

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

// Delete Provinces
// @Summary      Delete Provinces
// @Description  API for delete Provinces
// @Tags         Untags
// @Security     Bearer
// @Param        id path int true "Provinces 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       /provinces/{id} [delete]
func (_i *provincesController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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