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

/**
 * FlagEvaluation Entity
 * Maps to flags.flag_evaluations DDL table
 * Historial de evaluaciones de feature flags para analytics
 * Propagated from template-saas HU-REFACT-005
 */
@Entity({ schema: '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;

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