// Base interfaces for common parameters
export interface PaginationParams {
  page?: number;
  limit?: number;
}

export interface SortingParams {
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}

export interface PaginatedSortedParams
  extends PaginationParams,
    SortingParams {}

// URL Type enum for filtering
export enum UrlType {
  ARTICLE = 'article',
  LINK = 'link',
  BOOK = 'book',
  RESEARCH = 'research',
  AUDIO = 'audio',
  VIDEO = 'video',
  SOCIAL = 'social',
  EVENT = 'event',
  SOFTWARE = 'software',
}

// Activity Source enum for filtering
export enum ActivitySource {
  MARGIN = 'margin',
  SEMBLE = 'semble',
}

// Collection Access Type enum
export enum CollectionAccessType {
  OPEN = 'OPEN',
  CLOSED = 'CLOSED',
}

// Command request types
export interface AddUrlToLibraryRequest {
  url: string;
  note?: string;
  collectionIds?: string[];
  viaCardId?: string;
}

export interface AddCardToLibraryRequest {
  cardId: string;
  collectionIds?: string[];
}

export interface AddCardToCollectionRequest {
  cardId: string;
  collectionIds: string[];
}

export interface UpdateNoteCardRequest {
  cardId: string;
  note: string;
}

export interface UpdateUrlCardAssociationsRequest {
  cardId: string;
  note?: string;
  addToCollections?: string[];
  removeFromCollections?: string[];
  viaCardId?: string;
}

export interface RemoveCardFromLibraryRequest {
  cardId: string;
}

export interface RemoveCardFromCollectionRequest {
  cardId: string;
  collectionIds: string[];
}

export interface CreateCollectionRequest {
  name: string;
  description?: string;
  accessType?: CollectionAccessType;
}

export interface UpdateCollectionRequest {
  collectionId: string;
  name: string;
  description?: string;
  accessType?: CollectionAccessType;
}

export interface DeleteCollectionRequest {
  collectionId: string;
}

// Query parameters
export interface GetMyUrlCardsParams extends PaginatedSortedParams {
  urlType?: UrlType;
  uncollected?: boolean;
}

export interface GetUrlCardsParams extends PaginatedSortedParams {
  identifier: string; // Can be DID or handle
  urlType?: UrlType;
  uncollected?: boolean;
}

export interface GetCollectionPageParams extends PaginatedSortedParams {
  urlType?: UrlType;
}

export interface GetMyCollectionsParams extends PaginatedSortedParams {
  searchText?: string;
}

export interface GetCollectionsParams extends PaginatedSortedParams {
  identifier: string; // Can be DID or handle
  searchText?: string;
}

export interface GetCollectionPageByAtUriParams extends PaginatedSortedParams {
  handle: string;
  recordKey: string;
  urlType?: UrlType;
}

export interface GetGlobalFeedParams extends PaginationParams {
  beforeActivityId?: string; // For cursor-based pagination
  urlType?: UrlType; // Filter by URL type
  source?: ActivitySource; // Filter by activity source
  activityTypes?: string[]; // Filter by activity types
  includeKnownBots?: boolean; // Include known bots in feed (default: false)
}

export interface GetFollowingFeedParams extends PaginationParams {
  beforeActivityId?: string; // For cursor-based pagination
  urlType?: UrlType; // Filter by URL type
  source?: ActivitySource; // Filter by activity source
  activityTypes?: string[]; // Filter by activity types
  includeKnownBots?: boolean; // Include known bots in feed (default: false)
}

export interface LoginWithAppPasswordRequest {
  identifier: string;
  appPassword: string;
}

export interface InitiateOAuthSignInRequest {
  handle?: string;
}

export interface CompleteOAuthSignInRequest {
  code: string;
  state: string;
  iss: string;
}

export interface RefreshAccessTokenRequest {
  refreshToken: string;
}

export interface GenerateExtensionTokensRequest {
  // No additional parameters needed - user is authenticated via middleware
}

export interface GetProfileParams {
  identifier: string; // Can be DID or handle
  includeStats?: boolean; // Include card and collection counts
}

export interface GetUrlMetadataParams {
  url: string;
  includeStats?: boolean; // Optional flag to include aggregate statistics
}

export interface GetUrlStatusForMyLibraryParams {
  url: string;
}

export interface GetLibrariesForUrlParams extends PaginatedSortedParams {
  url: string;
}

export interface GetNoteCardsForUrlParams extends PaginatedSortedParams {
  url: string;
}

export interface GetCollectionsForUrlParams extends PaginatedSortedParams {
  url: string;
}

export interface GetSimilarUrlsForUrlParams extends PaginatedSortedParams {
  url: string;
  threshold?: number;
  urlType?: string;
}

export interface SemanticSearchUrlsParams extends PaginatedSortedParams {
  query: string;
  threshold?: number;
  urlType?: string;
  identifier?: string; // Can be DID or handle
}

export interface SearchBskyPostsForUrlParams {
  q: string;
  sort?: 'top' | 'latest' | string;
  since?: string;
  until?: string;
  mentions?: string;
  author?: string;
  lang?: string;
  domain?: string;
  url?: string;
  tag?: string[];
  limit?: number;
  cursor?: string;
}

export interface SearchAtProtoAccountsParams {
  /** DEPRECATED: use 'q' instead. */
  term?: string;
  /** Search query string. Syntax, phrase, boolean, and faceting is unspecified, but Lucene query syntax is recommended. */
  q?: string;
  limit?: number;
  cursor?: string;
}

export interface SearchLeafletDocsForUrlParams {
  url: string;
  limit?: number;
  cursor?: string;
}

export interface GetGemActivityFeedParams extends PaginationParams {
  // Removed beforeActivityId since we're using page-based pagination
  urlType?: UrlType; // Filter by URL type
  source?: ActivitySource; // Filter by activity source
  activityTypes?: string[]; // Filter by activity types
  includeKnownBots?: boolean; // Include known bots in feed (default: false)
}

export interface SearchCollectionsParams extends PaginatedSortedParams {
  searchText?: string;
  identifier?: string; // Can be DID or handle
  accessType?: CollectionAccessType;
}

export interface GetOpenCollectionsWithContributorParams
  extends PaginatedSortedParams {
  identifier: string; // Can be DID or handle
}

// Notification request types
export interface GetMyNotificationsParams extends PaginatedSortedParams {
  unreadOnly?: boolean;
}

export interface MarkNotificationsAsReadRequest {
  notificationIds: string[];
}

// Follow request types
export interface FollowTargetRequest {
  targetId: string; // DID or Collection UUID
  targetType: 'USER' | 'COLLECTION';
}

export interface UnfollowTargetRequest {
  targetId: string;
  targetType: 'USER' | 'COLLECTION';
}

// Follow query request types
export interface GetFollowingUsersParams extends PaginationParams {
  identifier: string; // Can be DID or handle
}

export interface GetFollowersParams extends PaginationParams {
  identifier: string; // Can be DID or handle
}

export interface GetFollowingCollectionsParams extends PaginationParams {
  identifier: string; // Can be DID or handle
}

export interface GetCollectionFollowersParams extends PaginationParams {
  collectionId: string; // Collection UUID
}

// Follow count request types
export interface GetFollowingCountParams {
  identifier: string; // Can be DID or handle
}

export interface GetFollowersCountParams {
  identifier: string; // Can be DID or handle
}

export interface GetFollowingCollectionsCountParams {
  identifier: string; // Can be DID or handle
}

export interface GetCollectionFollowersCountParams {
  collectionId: string; // Collection UUID
}

export interface GetCollectionContributorsParams extends PaginationParams {
  collectionId: string; // Collection UUID
}

// Connection types matching the domain model
export type ConnectionType =
  | 'SUPPORTS'
  | 'OPPOSES'
  | 'ADDRESSES'
  | 'HELPFUL'
  | 'LEADS_TO'
  | 'RELATED'
  | 'SUPPLEMENT'
  | 'EXPLAINER';

// Connection request types
export interface CreateConnectionRequest {
  sourceType: 'URL' | 'CARD';
  sourceValue: string;
  targetType: 'URL' | 'CARD';
  targetValue: string;
  connectionType?: ConnectionType;
  note?: string;
}

export interface UpdateConnectionRequest {
  connectionId: string;
  connectionType?: ConnectionType;
  note?: string;
  removeNote?: boolean;
  swap?: boolean;
}

export interface DeleteConnectionRequest {
  connectionId: string;
}

export interface GetConnectionsForUrlParams extends PaginatedSortedParams {
  url: string;
  direction?: 'forward' | 'backward' | 'both';
  connectionTypes?: ConnectionType[];
}

// Search URLs request types
export interface SearchUrlsParams extends PaginatedSortedParams {
  searchQuery: string;
  urlType?: UrlType;
}

// Get connections request types
export interface GetConnectionsParams extends PaginatedSortedParams {
  identifier: string; // Can be DID or handle
  connectionTypes?: ConnectionType[];
}

// Graph request types
export interface GetGraphDataParams extends PaginationParams {
  // Supports pagination for incremental graph loading
}

export interface GetUserGraphDataParams extends GetGraphDataParams {
  identifier: string; // Can be DID or handle
}

export interface GetUrlGraphDataParams {
  url: string; // Target URL to center the sub-graph around
  depth?: number; // Number of edge hops to traverse (1-5, defaults to 1)
}
