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

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

  @Index()
  @Column({ type: 'text' })
  teamId!: 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;
    description?: string;
  };

  @Column({ type: 'jsonb' })
  visibility!: {
    public: boolean;
  };

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