import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
} from 'typeorm';

export enum AnnouncementType {
  SYSTEM = 'SYSTEM',
  ACTIVITY = 'ACTIVITY',
  REWARD = 'REWARD',
  UPGRADE = 'UPGRADE',
  ANNOUNCEMENT = 'ANNOUNCEMENT',
  PROMOTION = 'PROMOTION',
}

export enum AnnouncementPriority {
  LOW = 'LOW',
  NORMAL = 'NORMAL',
  HIGH = 'HIGH',
  URGENT = 'URGENT',
}

export enum TargetType {
  ALL = 'ALL',
  BY_TAG = 'BY_TAG',
  SPECIFIC = 'SPECIFIC',
}

export interface AnnouncementTargetConfig {
  type: TargetType;
  tags?: string[];
  userIds?: string[];
}

@Entity('announcements')
@Index('idx_announcements_type', ['type'])
@Index('idx_announcements_target', ['targetType'])
@Index('idx_announcements_enabled', ['isEnabled', 'publishedAt'])
export class Announcement {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'varchar', length: 200 })
  title: string;

  @Column({ type: 'text' })
  content: string;

  @Column({ type: 'varchar', length: 20, default: AnnouncementType.SYSTEM })
  type: AnnouncementType;

  @Column({ type: 'varchar', length: 10, default: AnnouncementPriority.NORMAL })
  priority: AnnouncementPriority;

  @Column({ name: 'target_type', type: 'varchar', length: 10, default: TargetType.ALL })
  targetType: TargetType;

  @Column({ name: 'target_config', type: 'jsonb', nullable: true })
  targetConfig: AnnouncementTargetConfig | null;

  @Column({ name: 'image_url', type: 'varchar', length: 500, nullable: true })
  imageUrl: string | null;

  @Column({ name: 'link_url', type: 'varchar', length: 500, nullable: true })
  linkUrl: string | null;

  @Column({ name: 'is_enabled', type: 'boolean', default: true })
  isEnabled: boolean;

  @Column({ name: 'published_at', type: 'timestamptz', nullable: true })
  publishedAt: Date | null;

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

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

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

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

  // ── Domain Methods ──

  isActive(): boolean {
    if (!this.isEnabled || !this.publishedAt) {
      return false;
    }
    const now = new Date();
    if (this.publishedAt > now) {
      return false;
    }
    if (this.expiresAt && this.expiresAt < now) {
      return false;
    }
    return true;
  }

  isExpired(): boolean {
    if (!this.expiresAt) {
      return false;
    }
    return this.expiresAt < new Date();
  }

  isTargeted(): boolean {
    return this.targetType !== TargetType.ALL;
  }

  requiresTagFilter(): boolean {
    return (
      this.targetType === TargetType.BY_TAG &&
      !!this.targetConfig?.tags?.length
    );
  }

  isSpecificUsers(): boolean {
    return (
      this.targetType === TargetType.SPECIFIC &&
      !!this.targetConfig?.userIds?.length
    );
  }
}
