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

export type NotificationType =
  | 'SYSTEM'
  | 'MAINTENANCE'
  | 'FEATURE'
  | 'ANNOUNCEMENT'
  | 'BILLING'
  | 'SECURITY';

export type NotificationPriority = 'LOW' | 'NORMAL' | 'HIGH' | 'URGENT';

export type TargetType =
  | 'ALL'                // 全量广播
  | 'SPECIFIC_TENANTS'  // 定点：指定租户 ID 列表
  | 'SPECIFIC_USERS'    // 定点：指定用户 ID 列表（跨租户）
  | 'BY_TENANT_TAG'     // 标签推送：有 X 标签的租户
  | 'BY_PLAN'           // 分组推送：按订阅套餐
  | 'BY_TENANT_STATUS'  // 按租户状态
  | 'BY_SEGMENT';       // 预定义人群包

export type TagLogic = 'ANY' | 'ALL';

@Entity({ name: 'notifications', schema: 'public' })
export class Notification {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

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

  @Column({ type: 'varchar', length: 30, default: 'ANNOUNCEMENT' })
  @Index()
  type!: NotificationType;

  @Column({ type: 'varchar', length: 20, default: 'NORMAL' })
  priority!: NotificationPriority;

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

  // ── Targeting fields ──────────────────────────────────────────────────────

  /** For BY_TENANT_TAG: array of tag UUIDs */
  @Column({ name: 'target_tag_ids', type: 'uuid', array: true, nullable: true })
  targetTagIds!: string[] | null;

  /** For BY_TENANT_TAG: ANY (at least one tag) or ALL (every tag must match) */
  @Column({ name: 'target_tag_logic', type: 'varchar', length: 3, default: 'ANY' })
  targetTagLogic!: TagLogic;

  /** For BY_PLAN: e.g. ['free', 'pro'] */
  @Column({ name: 'target_plans', type: 'text', array: true, nullable: true })
  targetPlans!: string[] | null;

  /** For BY_TENANT_STATUS: e.g. ['active'] */
  @Column({ name: 'target_statuses', type: 'text', array: true, nullable: true })
  targetStatuses!: string[] | null;

  /** For BY_SEGMENT: segment key e.g. 'trial_ending_soon' */
  @Column({ name: 'target_segment', type: 'varchar', length: 100, nullable: true })
  targetSegment!: string | null;

  /** Notification channel key (Phase 2) — used for user opt-out filtering */
  @Column({ name: 'channel_key', type: 'varchar', length: 50, nullable: true })
  channelKey!: string | null;

  // ── Display fields ────────────────────────────────────────────────────────

  @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: 'requires_force_read', type: 'boolean', default: false })
  requiresForceRead!: boolean;

  @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: 'varchar', length: 100, nullable: true })
  createdBy!: string | null;

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

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