package controller

import (
	"strconv"

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

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

type SalesOrdersController struct {
	SalesOrdersService service.SalesOrdersService
}

func NewSalesOrdersController(salesOrdersService service.SalesOrdersService) *SalesOrdersController {
	return &SalesOrdersController{
		SalesOrdersService: salesOrdersService,
	}
}

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

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

func (u *SalesOrdersController) 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.SalesOrdersService.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 salesOrders successfully",
			Data:    dto.ToSalesOrdersListDTOFromMarketing(*result),
		})
}

func (u *SalesOrdersController) 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.SalesOrdersService.DeleteOne(c, uint(id)); err != nil {
		return err
	}

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

func (u *SalesOrdersController) Approval(c *fiber.Ctx) error {
	req := new(validation.Approve)

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

	results, err := u.SalesOrdersService.Approval(c, req)
	if err != nil {
		return err
	}

	var (
		data    interface{}
		message = "Submit sales order approval successfully"
	)
	if len(results) == 1 {
		data = dto.ToSalesOrdersListDTOFromMarketing(results[0])
	} else {
		message = "Submit sales order approvals successfully"
		data = dto.ToSalesOrdersListDTOsFromMarketing(results)
	}

	return c.Status(fiber.StatusOK).
		JSON(response.Success{
			Code:    fiber.StatusOK,
			Status:  "success",
			Message: message,
			Data:    data,
		})
}
