// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import type { AciString, PniString } from '../../types/ServiceId.std.ts';
import type { ConversationColorType } from '../../types/Colors.std.ts';
import type {
  CoreAttachmentBackupJobType,
  CoreAttachmentLocalBackupJobType,
} from '../../types/AttachmentBackup.std.ts';

// Duplicated here to allow loading it in a non-node environment
export enum BackupLevel {
  Free = 200,
  Paid = 201,
}

export function backupLevelFromNumber(
  num: number | undefined
): BackupLevel | null {
  if (Object.values(BackupLevel).includes(num as BackupLevel)) {
    return num as BackupLevel;
  }
  return null;
}

export type AboutMe = {
  aci: AciString;
  pni?: PniString;
  e164?: string;
};

export type OnProgressCallback = (
  currentBytes: number,
  totalBytes: number
) => void;

export type BackupExportOptions = { abortSignal: AbortSignal } & (
  | {
      type: 'remote' | 'cross-client-integration-test';
      level: BackupLevel;
    }
  | { type: 'plaintext-export' }
  | { type: 'local-encrypted' }
);

export type BackupImportOptions = (
  | { type: 'remote' | 'cross-client-integration-test' }
  | {
      type: 'local-encrypted';
      localBackupSnapshotDir: string;
    }
) & {
  ephemeralKey?: Uint8Array<ArrayBuffer>;
  onProgress?: OnProgressCallback;
};

export type LocalChatStyle = Readonly<{
  wallpaperPhotoPointer: Uint8Array<ArrayBuffer> | undefined;
  wallpaperPreset: number | undefined;
  color: ConversationColorType | undefined;
  customColorId: string | undefined;
  dimWallpaperInDarkMode: boolean | undefined;
  autoBubbleColor: boolean | undefined;
}>;

export type StatsType = {
  adHocCalls: number;
  callLinks: number;
  conversations: number;
  chatFolders: number;
  chats: number;
  distributionLists: number;
  fixedDirectMessages: number;
  messages: number;
  notificationProfiles: number;
  skippedConversations: number;
  skippedMessages: number;
  stickerPacks: number;
  unknownConversationReferences: Record<string, number>;
};

export type ExportResultType = Readonly<{
  attachmentBackupJobs: ReadonlyArray<
    CoreAttachmentBackupJobType | CoreAttachmentLocalBackupJobType
  >;
  mediaNames: Array<string>;
  totalBytes: number;
  duration: number;
  stats: Readonly<StatsType>;
}>;

export type LocalBackupExportResultType = ExportResultType & {
  snapshotDir: string;
};
