package service

import (
	"kra/internal/biz"
	"kra/internal/service/dto"
)

func convertAuthority(value biz.Authority) *dto.AuthorityResponse {
	var menus []*dto.MenuResponse
	if value.Menus != nil {
		menus = menuResponses(value.Menus)
	}
	return &dto.AuthorityResponse{CreatedAt: value.CreatedAt, UpdatedAt: value.UpdatedAt, DeletedAt: value.DeletedAt, AuthorityID: value.AuthorityID, AuthorityName: value.AuthorityName, ParentID: value.ParentID, Children: nil, Menus: menus, DataScope: value.DataScope, DefaultRouter: value.DefaultRouter}
}
func convertUser(user *biz.User) *dto.UserResponse {
	var authorities []*dto.AuthorityResponse
	if user.Authorities != nil {
		authorities = make([]*dto.AuthorityResponse, 0, len(user.Authorities))
		for _, value := range user.Authorities {
			authorities = append(authorities, convertAuthority(value))
		}
	}
	var departments []*dto.DepartmentResponse
	if user.Departments != nil {
		departments = make([]*dto.DepartmentResponse, 0, len(user.Departments))
		for i := range user.Departments {
			departments = append(departments, departmentResponse(&user.Departments[i]))
		}
	}
	var positions []*dto.PositionResponse
	if user.Positions != nil {
		positions = make([]*dto.PositionResponse, 0, len(user.Positions))
		for i := range user.Positions {
			positions = append(positions, positionResponse(&user.Positions[i]))
		}
	}
	// The primary department is represented by a zero-value object when it is
	// absent, preserving the response shape expected by the administration UI.
	var department any = dto.DepartmentResponse{}
	if user.Department != nil {
		department = departmentResponse(user.Department)
	}
	return &dto.UserResponse{ID: user.ID, CreatedAt: user.CreatedAt, UpdatedAt: user.UpdatedAt, DeletedAt: nil, UUID: user.UUID, Username: user.Username, NickName: user.NickName, HeaderImg: user.HeaderImg, AuthorityID: user.AuthorityID, Authority: convertAuthority(user.Authority), Authorities: authorities, DeptID: user.DeptID, Department: department, Departments: departments, Positions: positions, Phone: user.Phone, Email: user.Email, Enable: user.Enable, OriginSetting: user.OriginSetting, PasswordUpdatedAt: user.PasswordUpdatedAt}
}
