package dto

import (
	"time"

	entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
	uomDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/dto"
	userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)

// === DTO Structs ===

type NonstockRelationDTO struct {
	Id    uint                   `json:"id"`
	Name  string                 `json:"name"`
	Uom   *uomDTO.UomRelationDTO `json:"uom,omitempty"`
	Flags []string               `json:"flags"`
}

type NonstockListDTO struct {
	Id          uint                              `json:"id"`
	Name        string                            `json:"name"`
	Flags       []string                          `json:"flags"`
	Uom         *uomDTO.UomRelationDTO            `json:"uom"`
	Suppliers   []NonstockSupplierDTO             `json:"suppliers"`
	CreatedUser *userDTO.UserRelationDTO          `json:"created_user"`
	CreatedAt   time.Time                         `json:"created_at"`
	UpdatedAt   time.Time                         `json:"updated_at"`
}

type NonstockDetailDTO struct {
	NonstockListDTO
}

type NonstockSupplierDTO struct {
	Id       uint    `json:"id"`
	Name     string  `json:"name"`
	Alias    string  `json:"alias"`
	Category string  `json:"category"`
}

// === Mapper Functions ===

func ToNonstockRelationDTO(e entity.Nonstock) NonstockRelationDTO {
	var uomRef *uomDTO.UomRelationDTO
	if e.Uom.Id != 0 {
		mapped := uomDTO.ToUomRelationDTO(e.Uom)
		uomRef = &mapped
	}

	flags := make([]string, len(e.Flags))
	for i, f := range e.Flags {
		flags[i] = f.Name
	}

	return NonstockRelationDTO{
		Id:    e.Id,
		Name:  e.Name,
		Uom:   uomRef,
		Flags: flags,
	}
}

func ToNonstockListDTO(e entity.Nonstock) NonstockListDTO {
	var createdUser *userDTO.UserRelationDTO
	if e.CreatedUser.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
		createdUser = &mapped
	}

	var uomRef *uomDTO.UomRelationDTO
	if e.Uom.Id != 0 {
		mapped := uomDTO.ToUomRelationDTO(e.Uom)
		uomRef = &mapped
	}

	flags := make([]string, len(e.Flags))
	for i, f := range e.Flags {
		flags[i] = f.Name
	}

	return NonstockListDTO{
		Id:          e.Id,
		Name:        e.Name,
		Flags:       flags,
		Uom:         uomRef,
		Suppliers:   toNonstockSupplierDTOs(e.NonstockSuppliers),
		CreatedAt:   e.CreatedAt,
		UpdatedAt:   e.UpdatedAt,
		CreatedUser: createdUser,
	}
}

func ToNonstockListDTOs(e []entity.Nonstock) []NonstockListDTO {
	result := make([]NonstockListDTO, len(e))
	for i, r := range e {
		result[i] = ToNonstockListDTO(r)
	}
	return result
}

func ToNonstockDetailDTO(e entity.Nonstock) NonstockDetailDTO {
	return NonstockDetailDTO{
		NonstockListDTO: ToNonstockListDTO(e),
	}
}

func toNonstockSupplierDTOs(relations []entity.NonstockSupplier) []NonstockSupplierDTO {
	if len(relations) == 0 {
		return make([]NonstockSupplierDTO, 0)
	}

	result := make([]NonstockSupplierDTO, 0, len(relations))
	for _, relation := range relations {
		if relation.Supplier.Id == 0 {
			continue
		}
		result = append(result, NonstockSupplierDTO{
			Id:       relation.Supplier.Id,
			Name:     relation.Supplier.Name,
			Alias:    relation.Supplier.Alias,
			Category: relation.Supplier.Category,
		})
	}

	if len(result) == 0 {
		return make([]NonstockSupplierDTO, 0)
	}

	return result
}
