package controller

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

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

type citiesController struct {
	citiesService service.CitiesService
}

type CitiesController 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 NewCitiesController(citiesService service.CitiesService) CitiesController {
	return &citiesController{
		citiesService: citiesService,
	}
}

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

	var req request.CitiesQueryRequest
	req.Pagination = paginate

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

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

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

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

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

// Save Cities
// @Summary      Create Cities
// @Description  API for create Cities
// @Tags         Untags
// @Security     Bearer
// @Param 	     payload body request.CitiesCreateRequest true "Required payload"
// @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       /cities [post]
func (_i *citiesController) Save(c *fiber.Ctx) error {
	req := new(request.CitiesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

// Update Cities
// @Summary      Update Cities
// @Description  API for update Cities
// @Tags         Untags
// @Security     Bearer
// @Body 	     request.CitiesUpdateRequest
// @Param        id path int true "Cities ID"
// @Param 	     payload body request.CitiesUpdateRequest true "Required payload"
// @Accept       json
// @Produce      json
// @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       /cities/{id} [put]
func (_i *citiesController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

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

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

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

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