package controller

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

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

type districtsController struct {
	districtsService service.DistrictsService
}

type DistrictsController 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 NewDistrictsController(districtsService service.DistrictsService) DistrictsController {
	return &districtsController{
		districtsService: districtsService,
	}
}

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

	var req request.DistrictsQueryRequest
	req.Pagination = paginate

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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