import { Column, Entity, Index, PrimaryColumn } from 'typeorm';

/**
 * ORM Entity: RatingEvent
 * 
 * Stores rating events in the ledger with indexes for efficient querying
 * by userId and ordering by occurredAt for snapshot computation.
 */
@Entity({ name: 'rating_events' })
@Index(['userId', 'occurredAt', 'createdAt', 'id'], { unique: true })
export class RatingEventOrmEntity {
  @PrimaryColumn({ type: 'text' })
  id!: string;

  @Index()
  @Column({ type: 'text' })
  userId!: string;

  @Index()
  @Column({ type: 'text' })
  dimension!: string;

  @Column({ type: 'double precision' })
  delta!: number;

  @Column({ type: 'double precision', nullable: true })
  weight?: number;

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

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

  @Column({ type: 'jsonb' })
  source!: {
    type: 'race' | 'penalty' | 'vote' | 'adminAction' | 'manualAdjustment';
    id: string;
  };

  @Column({ type: 'jsonb' })
  reason!: {
    code: string;
    summary: string;
    details: Record<string, unknown>;
  };

  @Column({ type: 'jsonb' })
  visibility!: {
    public: boolean;
    redactedFields: string[];
  };

  @Column({ type: 'integer' })
  version!: number;
}