import type {
  Channel,
  ChannelPermissionOverride,
  RoomRole,
  RoomRoleAssignment
} from '../../../../shared-kernel';

export type ServerEndpointStatus = 'online' | 'offline' | 'checking' | 'unknown' | 'incompatible';

export interface ServerInfo {
  id: string;
  name: string;
  description?: string;
  topic?: string;
  hostName: string;
  ownerId?: string;
  ownerName?: string;
  ownerPublicKey?: string;
  userCount: number;
  maxUsers: number;
  icon?: string;
  iconUpdatedAt?: number;
  hasPassword?: boolean;
  isPrivate: boolean;
  tags?: string[];
  channels?: Channel[];
  slowModeInterval?: number;
  roles?: RoomRole[];
  roleAssignments?: RoomRoleAssignment[];
  channelPermissions?: ChannelPermissionOverride[];
  createdAt: number;
  sourceId?: string;
  sourceName?: string;
  sourceUrl?: string;
}

export interface ConfiguredDefaultServerDefinition {
  key?: string;
  name?: string;
  url?: string;
}

export interface DefaultServerDefinition {
  key: string;
  name: string;
  url: string;
}

export interface ServerEndpointVersions {
  serverInstanceId?: string | null;
  serverVersion?: string | null;
  clientVersion?: string | null;
}

export interface ServerEndpoint {
  id: string;
  instanceId?: string;
  name: string;
  url: string;
  isActive: boolean;
  isDefault: boolean;
  defaultKey?: string;
  status: ServerEndpointStatus;
  latency?: number;
  serverVersion?: string;
  clientVersion?: string;
}

export type DefaultEndpointTemplate = Omit<ServerEndpoint, 'id' | 'defaultKey'> & {
  defaultKey: string;
};

export interface ServerSourceSelector {
  sourceId?: string;
  sourceUrl?: string;
}

export interface ServerJoinAccessRequest {
  roomId: string;
  userId: string;
  userPublicKey: string;
  displayName: string;
  password?: string;
  inviteId?: string;
}

export interface ServerJoinAccessResponse {
  success: boolean;
  signalingUrl: string;
  joinedBefore: boolean;
  via: 'membership' | 'password' | 'invite' | 'public';
  server: ServerInfo;
}

export interface CreateServerInviteRequest {
  requesterUserId: string;
  requesterDisplayName?: string;
  requesterRole?: string;
}

export interface ServerInviteInfo {
  id: string;
  serverId: string;
  createdAt: number;
  expiresAt: number;
  inviteUrl: string;
  browserUrl: string;
  appUrl: string;
  sourceUrl: string;
  createdBy?: string;
  createdByDisplayName?: string;
  isExpired: boolean;
  server: ServerInfo;
}

export interface KickServerMemberRequest {
  actorUserId: string;
  actorRole?: string;
  targetUserId: string;
}

export interface BanServerMemberRequest extends KickServerMemberRequest {
  banId?: string;
  displayName?: string;
  reason?: string;
  expiresAt?: number;
}

export interface UnbanServerMemberRequest {
  actorUserId: string;
  actorRole?: string;
  banId?: string;
  targetUserId?: string;
}

export interface ServerVersionCompatibilityResult {
  isCompatible: boolean;
  serverVersion: string | null;
}

export interface ServerHealthCheckPayload {
  serverInstanceId?: unknown;
  serverVersion?: unknown;
}

export interface ServerEndpointHealthResult {
  status: ServerEndpointStatus;
  latency?: number;
  versions?: ServerEndpointVersions;
}
