/**
 * FlagEvaluation Entity
 * Feature flag evaluation history for analytics
 * Compatible with erp-core flag-evaluation.entity
 *
 * @module FeatureFlags
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Flag } from './flag.entity';

@Entity({ schema: 'feature_flags', name: 'flag_evaluations' })
@Index('idx_flag_evaluations_flag', ['flagId'])
@Index('idx_flag_evaluations_tenant', ['tenantId'])
@Index('idx_flag_evaluations_date', ['evaluatedAt'])
export class FlagEvaluation {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'uuid', nullable: false, name: 'flag_id' })
  flagId: string;

  @Column({ type: 'uuid', nullable: false, name: 'tenant_id' })
  tenantId: string;

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

  @Column({ type: 'boolean', nullable: false })
  result: boolean;

  @Column({ type: 'varchar', length: 100, nullable: true })
  variant: string | null;

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

  @Column({ type: 'varchar', length: 100, nullable: true, name: 'evaluation_reason' })
  evaluationReason: string | null;

  @Column({ type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP', name: 'evaluated_at' })
  evaluatedAt: Date;

  @ManyToOne(() => Flag, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'flag_id' })
  flag: Flag;
}
