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

@Entity({ name: 'tenant_overrides', schema: 'feature_flags' })
@Unique(['flagId', 'tenantId'])
export class TenantOverride {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

  @Index()
  @Column({ name: 'expires_at', type: 'timestamptz', nullable: true })
  expiresAt: Date;

  @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;

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