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

@Entity('telemetry_events')
@Index('idx_telemetry_events_name_time', ['eventName', 'eventTime'])
export class TelemetryEvent {
  @PrimaryGeneratedColumn({ type: 'bigint' })
  id: string;

  @Column({ name: 'user_id', type: 'uuid', nullable: true })
  userId: string | null;

  @Column({ name: 'install_id', length: 128 })
  installId: string;

  @Column({ name: 'event_name', length: 64 })
  eventName: string;

  @Column({ name: 'event_time', type: 'timestamptz' })
  eventTime: Date;

  // 设备字段：顶层独立列（B-tree 索引，千万级数据下按设备/版本分组查询毫秒级）
  @Column({ name: 'device_brand', length: 64, nullable: true })
  deviceBrand: string | null;

  @Column({ name: 'device_model', length: 64, nullable: true })
  deviceModel: string | null;

  @Column({ name: 'device_os', length: 32, nullable: true })
  deviceOs: string | null;

  @Column({ name: 'app_version', length: 32, nullable: true })
  appVersion: string | null;

  @Column({ length: 16, nullable: true })
  locale: string | null;

  @Column({ type: 'jsonb', default: '{}' })
  properties: Record<string, any>;

  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;
}
