import type { MetricWithComparison, TimestampedMetric, PaginatedResponse } from './common';

/**
 * Dashboard overview response
 */
export interface DashboardOverview {
  visitors: MetricWithComparison;
  pageViews: MetricWithComparison;
  sessions: MetricWithComparison;
  avgSessionDuration: MetricWithComparison;
  bounceRate: MetricWithComparison;
  conversions: MetricWithComparison;
}

/**
 * Time series data point
 */
export interface TimeSeriesDataPoint {
  timestamp: Date;
  value: number;
  groupKey?: string;
}

/**
 * Time series response
 */
export interface TimeSeriesResponse {
  data: TimeSeriesDataPoint[];
  total: number;
  granularity: string;
}

/**
 * Funnel step result
 */
export interface FunnelStepResult {
  stepNumber: number;
  stepName: string;
  count: number;
  conversionRate: number;
  dropoffRate: number;
  avgTimeToConvert?: number;
}

/**
 * Funnel analysis response
 */
export interface FunnelResponse {
  steps: FunnelStepResult[];
  overallConversionRate: number;
  totalStarted: number;
  totalCompleted: number;
}

/**
 * Cohort bucket
 */
export interface CohortBucket {
  cohortDate: Date;
  cohortSize: number;
  periods: CohortPeriod[];
}

/**
 * Cohort period data
 */
export interface CohortPeriod {
  periodNumber: number;
  value: number;
  percentage: number;
}

/**
 * Cohort analysis response
 */
export interface CohortResponse {
  cohorts: CohortBucket[];
  averageRetention: number[];
}

/**
 * Retention data point
 */
export interface RetentionDataPoint {
  period: string;
  retained: number;
  total: number;
  rate: number;
}

/**
 * Retention response
 */
export interface RetentionResponse {
  data: RetentionDataPoint[];
  d1: number;
  d7: number;
  d30: number;
}

/**
 * Top page entry
 */
export interface TopPageEntry {
  path: string;
  title?: string;
  views: number;
  uniqueVisitors: number;
  avgTimeOnPage: number;
  bounceRate: number;
}

/**
 * Top pages response
 */
export type TopPagesResponse = PaginatedResponse<TopPageEntry>;

/**
 * Revenue summary
 */
export interface RevenueSummary {
  total: MetricWithComparison;
  transactions: MetricWithComparison;
  avgOrderValue: MetricWithComparison;
  revenuePerUser: MetricWithComparison;
}

/**
 * Revenue by source
 */
export interface RevenueBySource {
  source: string;
  revenue: number;
  transactions: number;
  percentage: number;
}

/**
 * Revenue response
 */
export interface RevenueResponse {
  summary: RevenueSummary;
  timeSeries: TimestampedMetric[];
  bySource?: RevenueBySource[];
}

/**
 * Realtime metrics
 */
export interface RealtimeMetrics {
  activeUsers: number;
  pageViewsPerMinute: number;
  eventsPerMinute: number;
  topPages: Array<{ path: string; count: number }>;
  topReferrers: Array<{ referrer: string; count: number }>;
  lastUpdated: Date;
}

/**
 * User activity entry
 */
export interface UserActivityEntry {
  timestamp: Date;
  eventType: string;
  properties: Record<string, unknown>;
  sessionId: string;
  page?: string;
}

/**
 * User activity response
 */
export type UserActivityResponse = PaginatedResponse<UserActivityEntry>;

/**
 * Attribution data
 */
export interface AttributionReport {
  source: string;
  medium?: string;
  campaign?: string;
  visitors: number;
  conversions: number;
  revenue: number;
  conversionRate: number;
}

/**
 * Attribution response
 */
export interface AttributionResponse {
  data: AttributionReport[];
  topSource: string;
  topCampaign?: string;
}

/**
 * LTV (Lifetime Value) response
 */
export interface LtvResponse {
  averageLtv: number;
  ltvByCohort: Array<{
    cohort: string;
    ltv: number;
    customers: number;
  }>;
  projectedLtv: number;
}

/**
 * CAC (Customer Acquisition Cost) response
 */
export interface CacResponse {
  averageCac: number;
  cacByChannel: Array<{
    channel: string;
    cac: number;
    customers: number;
    spend: number;
  }>;
  ltvCacRatio: number;
}
