import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn, Index } from 'typeorm';
import { User } from '../../users/entities/user.entity';
import { ConnectedAccount } from './connected-account.entity';

/**
 * Meta Campaign Entity
 * كيان حملة ميتا
 * 
 * Caches campaign performance data in the database.
 * يخزن بيانات أداء الحملة في قاعدة البيانات.
 */
@Entity('meta_campaigns')
@Index(['campaignId', 'userId'], { unique: true })
export class MetaCampaign {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  campaignId: string; // The Meta-specific ID

  @Column()
  campaignName: string;

  @Column()
  status: string; // ACTIVE, PAUSED, etc.

  @Column({ type: 'bigint', default: 0 })
  impressions: number;

  @Column({ type: 'int', default: 0 })
  clicks: number;

  @Column({ type: 'float', default: 0 })
  spend: number;

  @Column({ type: 'float', default: 0 })
  ctr: number;

  @Column({ type: 'float', default: 0 })
  cpc: number;

  @Column({ type: 'float', default: 0 })
  cpm: number;

  @Column({ nullable: true })
  imageUrl: string;

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

  @Column({ nullable: true })
  platform: string; // facebook, instagram

  @Column({ type: 'jsonb', nullable: true })
  rawMetrics: any;

  @Column({ type: 'timestamp', nullable: true })
  lastSyncedAt: Date;

  @ManyToOne(() => User, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'userId' })
  user: User;

  @Column()
  userId: string;

  @ManyToOne(() => ConnectedAccount, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'accountId' })
  account: ConnectedAccount;

  @Column()
  accountId: string;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}
