import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
} from 'typeorm';
import { SegmentOperator, SegmentConditionDto } from './dto/segment.dto';

/**
 * Segment entity - stores saved segment definitions
 */
@Entity('segments')
@Index(['name'], { unique: true })
@Index(['isBuiltIn'])
export class Segment {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

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

  @Column({ type: 'jsonb' })
  conditions!: SegmentConditionDto[];

  @Column({
    type: 'enum',
    enum: SegmentOperator,
    default: SegmentOperator.AND,
  })
  operator!: SegmentOperator;

  @Column({ type: 'boolean', default: false })
  isBuiltIn!: boolean;

  @Column({ type: 'varchar', length: 64, nullable: true })
  createdBy?: string | null;

  @CreateDateColumn({ type: 'timestamptz' })
  createdAt!: Date;

  @UpdateDateColumn({ type: 'timestamptz' })
  updatedAt!: Date;
}
