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 BankRelationDTO struct {
	Id            uint    `json:"id"`
	Name          string  `json:"name"`
	Alias         string  `json:"alias"`
	Owner         *string `json:"owner"`
	AccountNumber string  `json:"account_number"`
}

type BankListDTO struct {
	BankRelationDTO
	CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
	CreatedAt   time.Time                `json:"created_at"`
	UpdatedAt   time.Time                `json:"updated_at"`
}

type BankDetailDTO struct {
	BankListDTO
}

// === Mapper Functions ===

func ToBankRelationDTO(e entity.Bank) BankRelationDTO {
	return BankRelationDTO{
		Id:            e.Id,
		Name:          e.Name,
		Alias:         e.Alias,
		Owner:         e.Owner,
		AccountNumber: e.AccountNumber,
	}
}

func ToBankListDTO(e entity.Bank) BankListDTO {
	var createdUser *userDTO.UserRelationDTO
	if e.CreatedUser.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
		createdUser = &mapped
	}

	return BankListDTO{
		BankRelationDTO: ToBankRelationDTO(e),
		CreatedAt:       e.CreatedAt,
		UpdatedAt:       e.UpdatedAt,
		CreatedUser:     createdUser,
	}
}

func ToBankListDTOs(e []entity.Bank) []BankListDTO {
	result := make([]BankListDTO, len(e))
	for i, r := range e {
		result[i] = ToBankListDTO(r)
	}
	return result
}

func ToBankDetailDTO(e entity.Bank) BankDetailDTO {
	return BankDetailDTO{
		BankListDTO: ToBankListDTO(e),
	}
}
