export interface Conversation {
  readonly id: string;
  readonly deviceId: string;
  readonly externalId: string;
  readonly displayName: string;
  readonly isGroup: boolean;
  readonly participants: readonly string[];
  readonly lastMessageAt: string | null;
  readonly messageCount: number;
  readonly syncedAt: string;
}

export type MessageProvider = 'imessage' | 'icloud';

export interface Message {
  readonly id: string;
  readonly deviceId: string;
  readonly conversationId: string;
  readonly externalId: string;
  readonly provider: MessageProvider;
  readonly fromHandle: string;
  readonly body: string;
  readonly isFromMe: boolean;
  readonly hasAttachments: boolean;
  readonly attachmentCount: number;
  readonly attachmentMimeTypes: readonly string[];
  readonly sentAt: string;
  readonly deliveredAt: string | null;
  readonly readAt: string | null;
  readonly syncedAt: string;
}

export type SearchMode = 'lexical' | 'semantic' | 'hybrid';

export interface MessageSearchHit {
  readonly message: Message;
  readonly rank: number;
  readonly snippet: string;
  readonly mode: SearchMode;
}

export interface MessageSearchResponse {
  readonly query: string;
  readonly mode: SearchMode;
  readonly limit: number;
  readonly offset: number;
  readonly total: number;
  /** ISO timestamp of when this search was executed on the server. */
  readonly searchDate: string;
  readonly results: readonly MessageSearchHit[];
}

export interface CalendarEvent {
  readonly id: string;
  readonly deviceId: string;
  readonly calendarId: string | null;
  readonly externalId: string;
  readonly title: string;
  readonly notes: string | null;
  readonly location: string | null;
  readonly url: string | null;
  readonly isAllDay: boolean;
  readonly startDate: string;
  readonly endDate: string;
  readonly status: string;
  readonly organizerName: string | null;
  readonly organizerEmail: string | null;
  readonly syncedAt: string;
}

export interface EventSearchHit {
  readonly event: CalendarEvent;
  readonly rank: number;
  readonly snippet: string;
}

export type ContactMatchField = 'display_name' | 'handle' | 'phone' | 'email';
export type ContactMatchStrength = 'exact' | 'prefix' | 'substring';
export interface ContactSearchHit {
  readonly contact: Contact;
  readonly matchField: ContactMatchField;
  readonly matchStrength: ContactMatchStrength;
  readonly rank: number;
}

export type UnifiedSurface = 'contact' | 'message' | 'mail' | 'calendar';

export type UnifiedSearchHit =
  | { surface: 'contact'; hit: ContactSearchHit; score: number }
  | { surface: 'message'; hit: MessageSearchHit; score: number }
  | { surface: 'mail'; hit: MessageSearchHit; score: number }
  | { surface: 'calendar'; hit: EventSearchHit; score: number };

export interface UnifiedSearchResponse {
  readonly query: string;
  readonly limit: number;
  readonly results: readonly UnifiedSearchHit[];
}

export interface ContactSummaryData {
  readonly mostRecently: string | null;
  readonly overallSummary: string | null;
  readonly recap: string | null;
}

export interface Contact {
  readonly id: string;
  readonly deviceId: string;
  readonly handle: string;
  readonly displayName: string;
  readonly phoneNumbers: readonly string[];
  readonly emails: readonly string[];
  readonly syncedAt: string;
  readonly updatedAt: string;
  readonly summaryData: ContactSummaryData | null;
  readonly summaryGeneratedAt: string | null;
}

export type PhotoMediaKind = 'image' | 'video' | 'live_photo' | 'audio';

export interface Photo {
  readonly id: string;
  readonly deviceId: string;
  readonly albumId: string | null;
  readonly externalId: string;
  readonly filename: string;
  readonly mediaType: string;
  readonly mediaKind: PhotoMediaKind | null;
  readonly mimeType: string | null;
  readonly width: number;
  readonly height: number;
  readonly duration: number | null;
  readonly takenAt: string | null;
  readonly modifiedAt: string | null;
  readonly importedAt: string;
  readonly storageBucket: string | null;
  readonly storageKey: string | null;
  readonly thumbnailKey: string | null;
  readonly previewKey: string | null;
  readonly latitude: number | null;
  readonly longitude: number | null;
  readonly locationName: string | null;
  readonly isFavorite: boolean;
  readonly isHidden: boolean;
  readonly isScreenshot: boolean;
  readonly isSelfie: boolean;
  readonly isBurst: boolean;
  readonly burstIdentifier: string | null;
  readonly exif: Readonly<Record<string, unknown>> | null;
  readonly contentHash: string | null;
  readonly processingStatus: 'pending' | 'processing' | 'completed' | 'failed' | 'skipped';
  readonly category: string | null;
  readonly semanticTags: readonly string[] | null;
  readonly faceCount: number;
  readonly syncedAt: string;
}

export interface Album {
  readonly id: string;
  readonly deviceId: string;
  readonly externalId: string;
  readonly name: string;
  readonly albumType: 'user' | 'smart' | 'shared' | 'system';
  readonly photoCount: number;
  readonly startDate: string | null;
  readonly endDate: string | null;
  readonly coverPhotoId: string | null;
  readonly sortOrder: number;
  readonly syncedAt: string;
}

export interface Identity {
  readonly id: string;
  readonly deviceId: string;
  readonly name: string | null;
  readonly faceCount: number;
  readonly centroidUpdatedAt: string | null;
  readonly createdAt: string;
  readonly updatedAt: string;
}

export interface IdentityPhotoListItem {
  readonly id: string;
  readonly thumbnailKey: string | null;
  readonly previewKey: string | null;
  readonly takenAt: string | null;
}
