namespace MyTelegram.Messenger.Handlers.LatestLayer.Impl.Messages;

///<summary>
/// Get info about chat invites generated by admins.
/// <para>Possible errors</para>
/// Code Type Description
/// 400 CHAT_ADMIN_REQUIRED You must be an admin in this chat to do this.
/// 403 CHAT_WRITE_FORBIDDEN You can't write in this chat.
/// 400 PEER_ID_INVALID The provided peer id is invalid.
/// See <a href="https://corefork.telegram.org/method/messages.getAdminsWithInvites" />
///</summary>
internal sealed class GetAdminsWithInvitesHandler(
    IQueryProcessor queryProcessor,
    IAccessHashHelper accessHashHelper,
    IUserConverterService userConverterService)
    : RpcResultObjectHandler<MyTelegram.Schema.Messages.RequestGetAdminsWithInvites,
            MyTelegram.Schema.Messages.IChatAdminsWithInvites>,
        Messages.IGetAdminsWithInvitesHandler
{
    protected override async Task<IChatAdminsWithInvites> HandleCoreAsync(IRequestInput input,
        RequestGetAdminsWithInvites obj)
    {
        if (obj.Peer is TInputPeerChannel inputPeerChannel)
        {
            await accessHashHelper.CheckAccessHashAsync(input, inputPeerChannel.ChannelId, inputPeerChannel.AccessHash, AccessHashType.Channel);
            var adminWithInvitesList =
                await queryProcessor.ProcessAsync(new GetAdminInvitesQuery(inputPeerChannel.ChannelId));
            var userIds = adminWithInvitesList.Select(p => p.AdminId).ToList();
            //var userReadModels = await userAppService.GetListAsync(userIds);
            //var contactReadModels = await queryProcessor.ProcessAsync(new GetContactListQuery(input.UserId, userIds));
            //var photoReadModels = await photoAppService.GetPhotosAsync(userReadModels, contactReadModels);
            //var privacyList = await privacyAppService.GetPrivacyListAsync(userIds);

            var users = await userConverterService.GetUserListAsync(input, userIds, false, false, input.Layer);

            return new TChatAdminsWithInvites
            {
                Admins = [.. adminWithInvitesList.Select(p => new TChatAdminWithInvites
                {
                    AdminId = p.AdminId,
                    InvitesCount = p.InvitesCount,
                    RevokedInvitesCount = p.RevokedInvitesCount,
                })],
                Users = [.. users]
            };
        }

        throw new NotImplementedException();
    }
}
