import type { EventType } from './events';
import type { TimeGranularity, TimeRange, PaginationParams } from './common';

/**
 * Base query parameters
 */
export interface BaseQueryParams {
  timeRange: TimeRange;
  filters?: QueryFilter[];
}

/**
 * Filter operator
 */
export type FilterOperator =
  | 'equals'
  | 'not_equals'
  | 'contains'
  | 'not_contains'
  | 'starts_with'
  | 'ends_with'
  | 'greater_than'
  | 'less_than'
  | 'in'
  | 'not_in';

/**
 * Query filter
 */
export interface QueryFilter {
  field: string;
  operator: FilterOperator;
  value: string | number | boolean | string[] | number[];
}

/**
 * Time series query parameters
 */
export interface TimeSeriesQueryParams extends BaseQueryParams {
  granularity: TimeGranularity;
  metric: string;
  groupBy?: string[];
}

/**
 * Funnel query parameters
 */
export interface FunnelQueryParams extends BaseQueryParams {
  steps: FunnelStep[];
  conversionWindow?: number; // seconds
}

/**
 * Funnel step definition
 */
export interface FunnelStep {
  name: string;
  eventType: EventType;
  filters?: QueryFilter[];
}

/**
 * Cohort query parameters
 */
export interface CohortQueryParams extends BaseQueryParams {
  cohortType: CohortType;
  granularity: TimeGranularity;
  metric: CohortMetric;
}

/**
 * Cohort type
 */
export type CohortType = 'first_visit' | 'signup' | 'first_purchase' | 'custom';

/**
 * Cohort metric
 */
export type CohortMetric =
  | 'retention'
  | 'revenue'
  | 'sessions'
  | 'conversions';

/**
 * Retention query parameters
 */
export interface RetentionQueryParams extends BaseQueryParams {
  retentionType: RetentionType;
  periods: number[];
}

/**
 * Retention type
 */
export type RetentionType = 'day' | 'week' | 'month';

/**
 * Top pages query parameters
 */
export interface TopPagesQueryParams extends BaseQueryParams, PaginationParams {
  metric: 'views' | 'unique_visitors' | 'avg_time_on_page';
  sortDirection: 'asc' | 'desc';
}

/**
 * Revenue query parameters
 */
export interface RevenueQueryParams extends BaseQueryParams {
  granularity: TimeGranularity;
  groupBy?: 'source' | 'medium' | 'campaign' | 'country';
}

/**
 * Realtime query parameters
 */
export interface RealtimeQueryParams {
  metrics: RealtimeMetric[];
  interval?: number; // seconds
}

/**
 * Realtime metric
 */
export type RealtimeMetric =
  | 'active_users'
  | 'page_views_per_minute'
  | 'events_per_minute'
  | 'top_pages'
  | 'top_referrers';

/**
 * User activity query parameters
 */
export interface UserActivityQueryParams extends BaseQueryParams, PaginationParams {
  userId?: string;
  sessionId?: string;
}
