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

export type ReferralStatus = 'PENDING' | 'ACTIVE' | 'REWARDED' | 'EXPIRED';

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

  // Referrer's tenant ID
  @Column({ name: 'referrer_tenant_id', type: 'varchar' })
  @Index()
  referrerTenantId!: string;

  // Referred tenant ID (unique: one tenant can only be referred once)
  @Column({ name: 'referred_tenant_id', type: 'varchar', unique: true })
  referredTenantId!: string;

  // The referral code that was used
  @Column({ name: 'referral_code', type: 'varchar', length: 20 })
  referralCode!: string;

  // Referral level: 1=direct, 2=indirect (referrer's referrer)
  @Column({ type: 'int', default: 1 })
  level!: number;

  // PENDING: registered but not paid
  // ACTIVE: first payment received
  // REWARDED: first-payment reward issued
  // EXPIRED: never paid within activation window
  @Column({ type: 'varchar', length: 20, default: 'PENDING' })
  @Index()
  status!: ReferralStatus;

  @CreateDateColumn({ name: 'registered_at', type: 'timestamptz' })
  registeredAt!: Date;

  // Timestamp of the referred tenant's first successful payment
  @Column({ name: 'activated_at', type: 'timestamptz', nullable: true })
  activatedAt!: Date | null;

  // Timestamp when first-payment reward was issued to referrer
  @Column({ name: 'rewarded_at', type: 'timestamptz', nullable: true })
  rewardedAt!: Date | null;
}
