package controller

import (
	"strconv"

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

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

type InjectionController struct {
	InjectionService service.InjectionService
}

func NewInjectionController(injectionService service.InjectionService) *InjectionController {
	return &InjectionController{
		InjectionService: injectionService,
	}
}

func (u *InjectionController) 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.InjectionService.GetOne(c, uint(id))
	if err != nil {
		return err
	}

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

func (u *InjectionController) 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.InjectionService.CreateOne(c, req)
	if err != nil {
		return err
	}

	return c.Status(fiber.StatusCreated).
		JSON(response.Success{
			Code:    fiber.StatusCreated,
			Status:  "success",
			Message: "Balance injection created successfully",
			Data:    dto.ToInjectionListDTO(*result),
		})
}

func (u *InjectionController) 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.InjectionService.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 injection successfully",
			Data:    dto.ToInjectionListDTO(*result),
		})
}
