using Hotline.Identity.Roles;
using Hotline.Share.Dtos.Roles;
using Hotline.Share.Dtos.Users;
using Hotline.Users;
using Mapster;

namespace Hotline.Application.Mappers;

public class IdentityMapperConfigs : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        #region identity

        config.ForType<AddUserDto, User>()
            .Map(d => d.Name, x => x.Name ?? x.UserName);

        config.ForType<UpdateUserDto, User>()
            .Map(d => d.Account, s => s);

        config.NewConfig<User, UserDto>()
            .IgnoreIf((s, d) => s.Account == null, d => d.UserName)
            .IgnoreIf((s, d) => s.Roles == null || !s.Roles.Any(), d => d.RoleNames)
            .IgnoreIf((s, d) => s.Organization == null, d => d.Organization)
            .Map(d => d.UserName, x => x.Account.UserName)
            .Map(d => d.State, x => x.IsDeleted ? "已删除" : "正常")
            .Map(d => d.RoleNames, x => string.Join(',', x.Roles.Select(s => s.DisplayName)))
            .IgnoreNullValues(true);

        config.NewConfig<Role, RoleDto>()
            .IgnoreIf((s, d) => s.Accounts == null, d => d.AccountIds)
            .Map(d => d.AccountIds, x => x.Accounts.Select(d => d.Id))
            .Map(d => d.State, x => x.IsDeleted ? "已删除" : "正常");

        #endregion
    }
}