package controller

import (
	"math"
	"strconv"

	"gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/dto"
	service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/services"
	validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/validations"
	"gitlab.com/mbugroup/lti-api.git/internal/response"

	"github.com/gofiber/fiber/v2"
)

type EmployeesController struct {
	EmployeesService service.EmployeesService
}

func NewEmployeesController(employeesService service.EmployeesService) *EmployeesController {
	return &EmployeesController{
		EmployeesService: employeesService,
	}
}

func (u *EmployeesController) GetAll(c *fiber.Ctx) error {
	query := &validation.Query{
		Page:   c.QueryInt("page", 1),
		Limit:  c.QueryInt("limit", 10),
		Search: c.Query("search", ""),
	}

	if query.Page < 1 || query.Limit < 1 {
		return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
	}

	if kandangParam := c.Query("kandang_id", ""); kandangParam != "" {
		id, err := strconv.Atoi(kandangParam)
		if err != nil || id <= 0 {
			return fiber.NewError(fiber.StatusBadRequest, "invalid kandang_id")
		}
		temp := uint(id)
		query.KandangId = &temp
	}

	if activeParam := c.Query("is_active", ""); activeParam != "" {
		value, err := strconv.ParseBool(activeParam)
		if err != nil {
			return fiber.NewError(fiber.StatusBadRequest, "invalid is_active value")
		}
		query.IsActive = &value
	}

	result, totalResults, err := u.EmployeesService.GetAll(c, query)
	if err != nil {
		return err
	}

	return c.Status(fiber.StatusOK).
		JSON(response.SuccessWithPaginate[dto.EmployeesListDTO]{
			Code:    fiber.StatusOK,
			Status:  "success",
			Message: "Get all employeess successfully",
			Meta: response.Meta{
				Page:         query.Page,
				Limit:        query.Limit,
				TotalPages:   int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
				TotalResults: totalResults,
			},
			Data: dto.ToEmployeesListDTOs(result),
		})
}

func (u *EmployeesController) GetOne(c *fiber.Ctx) error {
	param := c.Params("id")

	id, err := strconv.Atoi(param)
	if err != nil {
		return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
	}

	result, err := u.EmployeesService.GetOne(c, uint(id))
	if err != nil {
		return err
	}

	return c.Status(fiber.StatusOK).
		JSON(response.Success{
			Code:    fiber.StatusOK,
			Status:  "success",
			Message: "Get employees successfully",
			Data:    dto.ToEmployeesListDTO(*result),
		})
}

func (u *EmployeesController) CreateOne(c *fiber.Ctx) error {
	req := new(validation.Create)

	if err := c.BodyParser(req); err != nil {
		return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
	}

	result, err := u.EmployeesService.CreateOne(c, req)
	if err != nil {
		return err
	}

	return c.Status(fiber.StatusCreated).
		JSON(response.Success{
			Code:    fiber.StatusCreated,
			Status:  "success",
			Message: "Create employees successfully",
			Data:    dto.ToEmployeesListDTO(*result),
		})
}

func (u *EmployeesController) UpdateOne(c *fiber.Ctx) error {
	req := new(validation.Update)
	param := c.Params("id")

	id, err := strconv.Atoi(param)
	if err != nil {
		return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
	}

	if err := c.BodyParser(req); err != nil {
		return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
	}

	result, err := u.EmployeesService.UpdateOne(c, req, uint(id))
	if err != nil {
		return err
	}

	return c.Status(fiber.StatusOK).
		JSON(response.Success{
			Code:    fiber.StatusOK,
			Status:  "success",
			Message: "Update employees successfully",
			Data:    dto.ToEmployeesListDTO(*result),
		})
}

func (u *EmployeesController) DeleteOne(c *fiber.Ctx) error {
	param := c.Params("id")

	id, err := strconv.Atoi(param)
	if err != nil {
		return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
	}

	if err := u.EmployeesService.DeleteOne(c, uint(id)); err != nil {
		return err
	}

	return c.Status(fiber.StatusOK).
		JSON(response.Common{
			Code:    fiber.StatusOK,
			Status:  "success",
			Message: "Delete employees successfully",
		})
}
