import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as MongooseSchema } from 'mongoose';

export type AuditEventRollupDocument = AuditEventRollup & Document;

@Schema({ timestamps: true, collection: 'audit_event_rollups' })
export class AuditEventRollup {
  @Prop({ required: true })
  rollup_key: string;

  @Prop({ required: true, index: true })
  day: string;

  @Prop({ required: true, index: true })
  event_name: string;

  @Prop({ required: true, index: true })
  actor_id: string;

  @Prop({ required: true, enum: ['advisor', 'client', 'unknown'], index: true })
  actor_role: 'advisor' | 'client' | 'unknown';

  @Prop({ required: true, enum: ['web', 'api', 'mobile'], index: true })
  source: 'web' | 'api' | 'mobile';

  @Prop({ index: true })
  client_uuid?: string;

  @Prop({ required: true })
  count: number;

  @Prop({ required: true })
  first_seen_at: Date;

  @Prop({ required: true })
  last_seen_at: Date;

  @Prop({ required: true })
  rolled_up_until: Date;

  @Prop({ type: [String], default: [] })
  sample_session_ids: string[];

  @Prop({ type: [String], default: [] })
  sample_correlation_ids: string[];

  @Prop({ type: MongooseSchema.Types.Mixed, default: {} })
  metadata: Record<string, unknown>;
}

export const AuditEventRollupSchema =
  SchemaFactory.createForClass(AuditEventRollup);

AuditEventRollupSchema.index({ rollup_key: 1 }, { unique: true });
