package usecase

import (
	"gateway/internal/model/member/domain/entity"
	domusecase "gateway/internal/model/member/domain/usecase"
)

func memberToDTO(m *entity.Member) *domusecase.MemberDTO {
	if m == nil {
		return nil
	}
	return &domusecase.MemberDTO{
		TenantID:                m.TenantID,
		UID:                     m.UID,
		ZitadelEmail:            m.ZitadelEmail,
		ZitadelUserID:           m.ZitadelUserID,
		DisplayName:             m.DisplayName,
		Avatar:                  m.Avatar,
		Phone:                   m.Phone,
		Language:                m.Language,
		Currency:                m.Currency,
		Status:                  m.Status,
		Origin:                  m.Origin,
		BusinessEmail:           m.BusinessEmail,
		BusinessEmailVerified:   m.BusinessEmailVerified,
		BusinessEmailVerifiedAt: m.BusinessEmailVerifiedAt,
		BusinessPhone:           m.BusinessPhone,
		BusinessPhoneVerified:   m.BusinessPhoneVerified,
		BusinessPhoneVerifiedAt: m.BusinessPhoneVerifiedAt,
		TOTPEnrolled:            m.TOTPEnrolled,
		CreateAt:                m.CreateAt,
		UpdateAt:                m.UpdateAt,
	}
}

func tenantToDTO(t *entity.Tenant) *domusecase.TenantDTO {
	if t == nil {
		return nil
	}
	return &domusecase.TenantDTO{
		TenantID:  t.TenantID,
		Slug:      t.Slug,
		Name:      t.Name,
		UIDPrefix: t.UIDPrefix,
		Status:    string(t.Status),
		OrgID:     t.OrgID,
		CreateAt:  t.CreateAt,
		UpdateAt:  t.UpdateAt,
	}
}

func normalizeUIDPrefix(prefix string) string {
	out := make([]byte, 0, len(prefix))
	for i := 0; i < len(prefix); i++ {
		c := prefix[i]
		if c >= 'a' && c <= 'z' {
			c -= 'a' - 'A'
		}
		if (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') {
			out = append(out, c)
		}
	}
	return string(out)
}

func defaultLanguage(lang string) string {
	if lang != "" {
		return lang
	}
	return "zh-tw"
}
