package controller

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

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

type usersController struct {
	usersService service.UsersService
}

type UsersController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Login(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewUsersController(usersService service.UsersService) UsersController {
	return &usersController{
		usersService: usersService,
	}
}

// All Users
// @Summary      Get all Users
// @Description  API for getting all Users
// @Tags         Users
// @Security     Bearer
// @Param 	     req query request.UsersQueryRequest false "query parameters"
// @Param 	     req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /users [get]
func (_i *usersController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.UsersQueryRequestContext{
		Username:       c.Query("username"),
		Email:          c.Query("email"),
		Fullname:       c.Query("fullname"),
		Address:        c.Query("address"),
		PhoneNumber:    c.Query("phoneNumber"),
		IdentityNumber: c.Query("identityNumber"),
		UserRoleId:     c.Query("userRoleId"),
		StatusId:       c.Query("statusId"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Users list successfully retrieved"},
		Data:     usersData,
		Meta:     paging,
	})
}

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

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

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

// Save Users
// @Summary      Create Users
// @Description  API for create Users
// @Tags         Users
// @Security     Bearer
// @Param        Authorization header string true "Insert your access token" default(Bearer <Add access token here>)
// @Param 	     payload body request.UsersCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /users [post]
func (_i *usersController) Save(c *fiber.Ctx) error {
	req := new(request.UsersCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	authToken := c.Get("Authorization")

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

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

// Update Users
// @Summary      update Users
// @Description  API for update Users
// @Tags         Users
// @Security     Bearer
// @Param 	     payload body request.UsersUpdateRequest true "Required payload"
// @Param        id path int true "Users ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /users/{id} [put]
func (_i *usersController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

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

// Login Users
// @Summary      Login Users
// @Description  API for Login Users
// @Tags         Users
// @Security     Bearer
// @Param 	     payload body request.UserLogin true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /users/login [post]
func (_i *usersController) Login(c *fiber.Ctx) error {
	req := new(request.UserLogin)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	loginResponse, err := _i.usersService.Login(*req)
	if err != nil {
		return utilRes.Resp(c, utilRes.Response{
			Success:  false,
			Code:     401,
			Messages: utilRes.Messages{err.Error()},
		})
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Users successfully login"},
		Data:     loginResponse,
	})
}

// Delete Users
// @Summary      delete Users
// @Description  API for delete Users
// @Tags         Users
// @Security     Bearer
// @Param        id path int true "Users ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /users/{id} [delete]
func (_i *usersController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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