/**
 * Flag Entity
 * Feature flag definition with rollout control
 * Compatible with erp-core flag.entity
 *
 * @module FeatureFlags
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  Unique,
  OneToMany,
} from 'typeorm';
import { TenantOverride } from './tenant-override.entity';

@Entity({ name: 'flags', schema: 'feature_flags' })
@Unique(['code'])
export class Flag {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'code', type: 'varchar', length: 50 })
  code: string;

  @Column({ name: 'name', type: 'varchar', length: 100 })
  name: string;

  @Column({ name: 'description', type: 'text', nullable: true })
  description: string;

  @Index()
  @Column({ name: 'enabled', type: 'boolean', default: false })
  enabled: boolean;

  @Column({ name: 'rollout_percentage', type: 'int', default: 100 })
  rolloutPercentage: number;

  @Column({ name: 'tags', type: 'text', array: true, nullable: true })
  tags: string[];

  @Index()
  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive: boolean;

  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt: Date;

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

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

  @OneToMany(() => TenantOverride, (override) => override.flag)
  overrides: TenantOverride[];
}
