export interface IJellyfinConfig {
  url?: string;
  host?: string;
  port?: number;
  ssl?: boolean;
  name?: string;
  uniqueId?: string;
  accessToken?: string;
  apiToken?: string;
  username?: string;
  password?: string;
  userId?: string;
  clientDeviceId?: string;
  deviceName?: string;
  timeoutMs?: number;
  activeWithinSeconds?: number;
  server?: IJellyfinServerInfo;
  sessions?: IJellyfinSession[];
  users?: IJellyfinUser[];
  snapshot?: IJellyfinSnapshot;
}

export interface IHomeAssistantJellyfinConfig extends IJellyfinConfig {}

export interface IJellyfinServerInfo {
  Id?: string;
  ServerId?: string;
  Name?: string;
  ServerName?: string;
  Version?: string;
  ProductName?: string;
  OperatingSystem?: string;
  LocalAddress?: string;
  WanAddress?: string;
  StartupWizardCompleted?: boolean;
  [key: string]: unknown;
}

export interface IJellyfinUser {
  Id: string;
  Name?: string;
  ServerId?: string;
  PrimaryImageTag?: string;
  HasPassword?: boolean;
  HasConfiguredPassword?: boolean;
  [key: string]: unknown;
}

export interface IJellyfinSessionCapabilities {
  SupportsMediaControl?: boolean;
  SupportsPersistentIdentifier?: boolean;
  SupportsSync?: boolean;
  SupportsContentUploading?: boolean;
  SupportedCommands?: TJellyfinGeneralCommand[];
  [key: string]: unknown;
}

export interface IJellyfinPlayState {
  PositionTicks?: number;
  CanSeek?: boolean;
  IsPaused?: boolean;
  IsMuted?: boolean;
  VolumeLevel?: number;
  AudioStreamIndex?: number;
  SubtitleStreamIndex?: number;
  MediaSourceId?: string;
  PlayMethod?: 'Transcode' | 'DirectStream' | 'DirectPlay' | string;
  RepeatMode?: string;
  [key: string]: unknown;
}

export interface IJellyfinMediaSource {
  Id?: string;
  Path?: string;
  Protocol?: string;
  Container?: string;
  Size?: number;
  Bitrate?: number;
  RunTimeTicks?: number;
  VideoType?: string;
  [key: string]: unknown;
}

export interface IJellyfinMediaItem {
  Id?: string;
  Name?: string;
  Type?: string;
  MediaType?: string;
  RunTimeTicks?: number;
  SeriesName?: string;
  ParentIndexNumber?: number;
  IndexNumber?: number;
  Album?: string;
  AlbumArtist?: string;
  Artists?: string[];
  Overview?: string;
  ProductionYear?: number;
  PremiereDate?: string;
  CommunityRating?: number;
  OfficialRating?: string;
  ImageTags?: Record<string, string | undefined>;
  ParentBackdropItemId?: string;
  AlbumId?: string;
  AlbumPrimaryImageTag?: string;
  MediaSources?: IJellyfinMediaSource[];
  [key: string]: unknown;
}

export interface IJellyfinSession {
  Id: string;
  UserId?: string;
  UserName?: string;
  Client?: string;
  LastActivityDate?: string;
  LastPlaybackCheckIn?: string;
  DeviceName?: string;
  DeviceId?: string;
  ApplicationVersion?: string;
  RemoteEndPoint?: string;
  IsActive?: boolean;
  SupportsRemoteControl?: boolean;
  PlayState?: IJellyfinPlayState;
  NowPlayingItem?: IJellyfinMediaItem;
  NowViewingItem?: IJellyfinMediaItem;
  TranscodingInfo?: Record<string, unknown>;
  Capabilities?: IJellyfinSessionCapabilities;
  [key: string]: unknown;
}

export interface IJellyfinSnapshot {
  server: IJellyfinServerInfo;
  sessions: IJellyfinSession[];
  users?: IJellyfinUser[];
  online: boolean;
  updatedAt?: string;
}

export interface IJellyfinAuthenticationResult {
  AccessToken?: string;
  ServerId?: string;
  User?: IJellyfinUser;
  SessionInfo?: IJellyfinSession;
  [key: string]: unknown;
}

export type TJellyfinPlaystateCommand =
  | 'Stop'
  | 'Pause'
  | 'Unpause'
  | 'NextTrack'
  | 'PreviousTrack'
  | 'Seek'
  | 'Rewind'
  | 'FastForward'
  | 'PlayPause';

export type TJellyfinPlayCommand = 'PlayNow' | 'PlayNext' | 'PlayLast' | 'PlayInstantMix' | 'PlayShuffle';

export type TJellyfinGeneralCommand = string;

export interface IJellyfinGeneralCommandRequest {
  Name: TJellyfinGeneralCommand;
  ControllingUserId?: string;
  Arguments?: Record<string, string>;
}

export interface IJellyfinEvent {
  type: 'sessions' | 'playstate' | 'general_command' | 'keepalive' | 'error' | string;
  sessionId?: string;
  data?: unknown;
  timestamp?: number;
}

export interface IJellyfinManualEntry {
  url?: string;
  host?: string;
  port?: number;
  ssl?: boolean;
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  metadata?: Record<string, unknown>;
}

export interface IJellyfinMdnsRecord {
  type?: string;
  name?: string;
  host?: string;
  port?: number;
  addresses?: string[];
  hostname?: string;
  txt?: Record<string, string | undefined>;
  properties?: Record<string, string | undefined>;
}

export interface IJellyfinSsdpRecord {
  st?: string;
  usn?: string;
  location?: string;
  server?: string;
  headers?: Record<string, string | undefined>;
}

export interface IJellyfinDiscoveryMetadata {
  url?: string;
  ssl?: boolean;
  ssdpSt?: string;
  ssdpUsn?: string;
  mdnsType?: string;
  [key: string]: unknown;
}
