import { Entity, PrimaryColumn, Column, UpdateDateColumn } from 'typeorm';

/**
 * Denormalized stats cache per tenant — updated after each referral event.
 * Avoids expensive COUNT queries on hot paths.
 */
@Entity({ name: 'referral_stats', schema: 'public' })
export class ReferralStat {
  // tenant_id is the PK (one row per tenant)
  @PrimaryColumn({ name: 'tenant_id', type: 'varchar' })
  tenantId!: string;

  // Number of direct referrals (registered, any status)
  @Column({ name: 'direct_count', type: 'int', default: 0 })
  directCount!: number;

  // Number of referrals that paid at least once (ACTIVE or REWARDED)
  @Column({ name: 'active_count', type: 'int', default: 0 })
  activeCount!: number;

  // Total credits earned (cents), including applied and pending
  @Column({ name: 'total_credit_earned', type: 'int', default: 0 })
  totalCreditEarned!: number;

  // Credits already deducted from invoices
  @Column({ name: 'total_credit_applied', type: 'int', default: 0 })
  totalCreditApplied!: number;

  // Credits waiting to be applied to the next invoice
  @Column({ name: 'pending_credit', type: 'int', default: 0 })
  pendingCredit!: number;

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