namespace Ikar.Shared;

public enum ChatMemberRole
{
    Member = 0,
    Admin = 1,
    Owner = 2
}

public sealed record ChatPermissionsDto(
    bool CanPostMessages,
    bool CanPinMessages,
    bool CanDeleteMessages,
    bool CanInviteUsers,
    bool CanManageMembers);

public sealed record ChatMemberDto(
    UserSummaryDto User,
    ChatMemberRole Role,
    bool IsOwner,
    ChatPermissionsDto Permissions);

public sealed record ChatSummaryDto(
    Guid Id,
    string Title,
    ChatType Type,
    string? SecondaryText,
    DateTimeOffset? LastActivityAt,
    string? LastMessagePreview,
    IReadOnlyList<UserSummaryDto> Participants,
    bool CanSendMessages,
    int UnreadCount,
    bool IsPinned = false,
    bool IsArchived = false,
    bool IsMuted = false,
    string? FolderKey = null,
    bool HasProtectedContent = false,
    int? DefaultMessageTtlSeconds = null,
    AttachmentDto? LastMessageAttachment = null,
    int SubscriberCount = 0,
    int AdminCount = 0,
    bool IsSubscribed = false,
    bool CanSubscribe = false,
    bool CanManageChannel = false,
    bool CanPostAsChannel = false,
    string? PublicUsername = null,
    Guid? LinkedDiscussionChatId = null,
    bool ChannelSignaturesEnabled = false,
    string? Description = null);

public sealed record ChatDetailsDto(
    Guid Id,
    string Title,
    ChatType Type,
    IReadOnlyList<UserSummaryDto> Participants,
    bool CanSendMessages,
    IReadOnlyList<MessageDto> Messages,
    Guid? FirstUnreadMessageId = null,
    bool HasMoreMessages = false,
    IReadOnlyList<MessageDto>? PinnedMessages = null,
    ChatPermissionsDto? CurrentUserPermissions = null,
    IReadOnlyList<ChatMemberDto>? Members = null,
    bool HasProtectedContent = false,
    int? DefaultMessageTtlSeconds = null,
    int SubscriberCount = 0,
    int AdminCount = 0,
    bool IsSubscribed = false,
    bool CanSubscribe = false,
    bool CanManageChannel = false,
    bool CanPostAsChannel = false,
    string? PublicUsername = null,
    Guid? LinkedDiscussionChatId = null,
    bool ChannelSignaturesEnabled = false,
    string? Description = null);

public sealed record ChatInfoDto(
    ChatDetailsDto Chat,
    int MediaCount,
    int FileCount,
    int LinkCount,
    int VoiceCount,
    int PinnedCount,
    int TopicCount,
    string? PublicUsername,
    IReadOnlyList<ChatInviteLinkDto> InviteLinks,
    IReadOnlyList<ChatTopicDto> Topics,
    int SubscriberCount = 0,
    int AdminCount = 0,
    Guid? LinkedDiscussionChatId = null,
    bool ChannelSignaturesEnabled = false,
    bool CanManageChannel = false,
    bool CanPostAsChannel = false,
    string? Description = null);

public sealed record ChatMessagesPageDto(
    IReadOnlyList<MessageDto> Messages,
    bool HasMoreMessages);

public sealed record ChatFolderDto(
    string Key,
    string Title,
    int TotalCount,
    int UnreadCount);

public sealed record ChatStateRequest(
    bool? IsPinned = null,
    bool? IsArchived = null,
    DateTimeOffset? MutedUntil = null,
    string? FolderKey = null,
    bool? IsMuted = null,
    bool? HasProtectedContent = null,
    int? DefaultMessageTtlSeconds = null,
    bool? ChannelSignaturesEnabled = null,
    Guid? LinkedDiscussionChatId = null);

public sealed record UpdateChatMemberPermissionsRequest(
    ChatMemberRole Role,
    bool CanPinMessages,
    bool CanDeleteMessages,
    bool CanInviteUsers,
    bool CanPostMessages);

public sealed record AddChatMembersRequest(
    IReadOnlyList<Guid> MemberIds,
    ChatMemberRole Role = ChatMemberRole.Member,
    bool CanPinMessages = false,
    bool CanDeleteMessages = false,
    bool CanInviteUsers = false,
    bool CanPostMessages = false);

public sealed record ChatInviteLinkDto(
    Guid Id,
    Guid ChatId,
    string Token,
    string Link,
    string? Title,
    bool CreatesJoinRequest,
    bool IsRevoked,
    DateTimeOffset CreatedAt,
    DateTimeOffset? RevokedAt = null);

public sealed record CreateChatInviteLinkRequest(
    string? Title = null,
    bool CreatesJoinRequest = false);

public sealed record ChatTopicDto(
    Guid Id,
    Guid ChatId,
    string Title,
    string Icon,
    bool IsPinned,
    bool IsClosed,
    DateTimeOffset CreatedAt,
    DateTimeOffset LastActivityAt,
    int MessageCount);

public sealed record CreateChatTopicRequest(
    string Title,
    string? Icon = null);

public sealed record ChatSearchResultDto(
    ChatSummaryDto Chat,
    MessageDto? Message,
    string MatchText,
    DateTimeOffset? MatchedAt,
    Guid? ChatId = null,
    Guid? MessageId = null,
    Guid? SenderUserId = null,
    DateTimeOffset? MessageSentAt = null);

public sealed record ChatSearchPageDto(
    IReadOnlyList<ChatSearchResultDto> Results,
    int Page,
    int Limit,
    bool HasMore);

public sealed record MessageSearchPageDto(
    IReadOnlyList<MessageDto> Messages,
    int Page,
    int Limit,
    bool HasMore);

public sealed record TypingIndicatorDto(
    Guid ChatId,
    UserSummaryDto User,
    bool IsTyping,
    DateTimeOffset TypedAt);

public sealed record CreateDirectChatRequest(Guid UserId);

public sealed record CreateGroupChatRequest(string Title, IReadOnlyList<Guid> MemberIds);

public sealed record CreateChannelRequest(
    string Title,
    IReadOnlyList<Guid> MemberIds,
    string? Description = null,
    string? PublicUsername = null,
    bool CreateInviteLink = false,
    bool ChannelSignaturesEnabled = false);

public enum ChatJoinRequestStatus
{
    Pending = 0,
    Approved = 1,
    Declined = 2
}

public sealed record ChatJoinRequestDto(
    Guid Id,
    Guid ChatId,
    Guid InviteLinkId,
    UserSummaryDto User,
    ChatJoinRequestStatus Status,
    DateTimeOffset RequestedAt,
    DateTimeOffset? ResolvedAt,
    UserSummaryDto? ResolvedBy);

public sealed record ResolveChatJoinRequestRequest(bool Approve);
