import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne } from 'typeorm';
import { User } from '../../users/entities/user.entity';

export enum RuleAction {
  PAUSE = 'pause',
  NOTIFY = 'notify',
  SCALE_DOWN = 'scale_down',
}

export enum RuleConditionMetric {
  SPEND = 'spend',
  ROAS = 'roas',
  CPA = 'cpa',
  CTR = 'ctr',
  CLICKS = 'clicks',
}

export enum RuleConditionOperator {
  GREATER_THAN = '>',
  LESS_THAN = '<',
  EQUAL = '=',
}

@Entity('rules')
export class Rule {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  name: string;

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

  @Column({ type: 'enum', enum: RuleAction, default: RuleAction.NOTIFY })
  action: RuleAction;

  @Column({ type: 'jsonb' })
  conditions: {
    metric: RuleConditionMetric;
    operator: RuleConditionOperator;
    value: number;
    timeRange: string; // e.g., 'last_7_days', 'today'
  }[];

  @Column()
  platform: 'meta' | 'tiktok' | 'google';

  @Column({ nullable: true })
  targetId: string; // ID of the campaign, adset, or ad to monitor. 'all' for everything.

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @ManyToOne(() => User, (user) => user.id)
  user: User;
}
