import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  Index,
} from 'typeorm';

export enum MetricType {
  // Core metrics
  PAGE_VIEWS = 'page_views',
  UNIQUE_VISITORS = 'unique_visitors',
  SESSIONS = 'sessions',
  EVENT_COUNT = 'event_count',
  CONVERSION_RATE = 'conversion_rate',
  REVENUE = 'revenue',

  // Engagement metrics
  ENGAGED_SESSIONS = 'engaged_sessions',
  ENGAGEMENT_RATE = 'engagement_rate',
  AVG_SESSION_DURATION = 'avg_session_duration',
  PAGES_PER_SESSION = 'pages_per_session',
  BOUNCE_RATE = 'bounce_rate',

  // Acquisition metrics
  NEW_USERS = 'new_users',
  RETURNING_USERS = 'returning_users',
}

export enum TimeGranularity {
  MINUTE = 'minute',
  HOUR = 'hour',
  DAY = 'day',
  WEEK = 'week',
  MONTH = 'month',
}

@Entity('aggregated_metrics')
@Index(['metricType', 'granularity', 'timestamp'])
@Index(['metricType', 'dimension', 'dimensionValue', 'timestamp'])
export class AggregatedMetric {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  @Column({
    type: 'enum',
    enum: MetricType,
  })
  @Index()
  metricType!: MetricType;

  @Column({
    type: 'enum',
    enum: TimeGranularity,
  })
  granularity!: TimeGranularity;

  @Column({ type: 'timestamptz' })
  @Index()
  timestamp!: Date;

  @Column({ type: 'decimal', precision: 20, scale: 4, default: 0 })
  value!: number;

  @Column({ type: 'bigint', default: 0 })
  count!: number;

  @Column({ nullable: true })
  dimension?: string;

  @Column({ nullable: true })
  dimensionValue?: string;

  @Column({ type: 'jsonb', nullable: true })
  metadata?: Record<string, unknown>;

  @CreateDateColumn({ type: 'timestamptz' })
  createdAt!: Date;
}
