package dto

import (
	"time"

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

// === DTO Structs ===

type CustomerBaseDTO struct {
	Id            uint    `json:"id"`
	Name          string  `json:"name"`
	PicId         uint    `json:"pic_id"`
	Type          string  `json:"type"`
	Address       string  `json:"address"`
	Phone         string  `json:"phone"`
	Email         string  `json:"email"`
	AccountNumber string  `json:"account_number"`
	Balance       float64 `json:"balance"`

	Pic *userDTO.UserBaseDTO `json:"pic"`
}

type CustomerListDTO struct {
	CustomerBaseDTO
	CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
	CreatedAt   time.Time            `json:"created_at"`
	UpdatedAt   time.Time            `json:"updated_at"`
}

type CustomerDetailDTO struct {
	CustomerListDTO
}

// === Mapper Functions ===

func ToCustomerBaseDTO(e entity.Customer) CustomerBaseDTO {
	var pic *userDTO.UserBaseDTO
	if e.Pic.Id != 0 {
		mapped := userDTO.ToUserBaseDTO(e.Pic)
		pic = &mapped
	}

	return CustomerBaseDTO{
		Id:            e.Id,
		Name:          e.Name,
		PicId:         e.PicId,
		Type:          e.Type,
		Address:       e.Address,
		Phone:         e.Phone,
		Email:         e.Email,
		AccountNumber: e.AccountNumber,
		Pic:           pic,
	}
}

func ToCustomerListDTO(e entity.Customer) CustomerListDTO {
	var createdUser *userDTO.UserBaseDTO
	if e.CreatedUser.Id != 0 {
		mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
		createdUser = &mapped
	}

	return CustomerListDTO{
		CustomerBaseDTO: ToCustomerBaseDTO(e),
		CreatedAt:       e.CreatedAt,
		UpdatedAt:       e.UpdatedAt,
		CreatedUser:     createdUser,
	}
}

func ToCustomerListDTOs(e []entity.Customer) []CustomerListDTO {
	result := make([]CustomerListDTO, len(e))
	for i, r := range e {
		result[i] = ToCustomerListDTO(r)
	}
	return result
}

func ToCustomerDetailDTO(e entity.Customer) CustomerDetailDTO {
	return CustomerDetailDTO{
		CustomerListDTO: ToCustomerListDTO(e),
	}
}
