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

export type BillingAlertType =
  | 'usage_limit'
  | 'payment_due'
  | 'payment_failed'
  | 'trial_ending'
  | 'subscription_ending';

export type AlertSeverity = 'info' | 'warning' | 'critical';
export type AlertStatus = 'active' | 'acknowledged' | 'resolved';

/**
 * Entidad para alertas de facturacion y limites de uso.
 * Mapea a billing.billing_alerts (DDL: 05-billing-usage.sql)
 */
@Entity({ name: 'billing_alerts', schema: 'billing' })
export class BillingAlert {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

  // Tipo de alerta
  @Index()
  @Column({ name: 'alert_type', type: 'varchar', length: 30 })
  alertType: BillingAlertType;

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

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

  @Column({ type: 'varchar', length: 20, default: 'info' })
  severity: AlertSeverity;

  // Estado
  @Index()
  @Column({ type: 'varchar', length: 20, default: 'active' })
  status: AlertStatus;

  // Notificacion
  @Column({ name: 'notified_at', type: 'timestamptz', nullable: true })
  notifiedAt: Date;

  @Column({ name: 'acknowledged_at', type: 'timestamptz', nullable: true })
  acknowledgedAt: Date;

  @Column({ name: 'acknowledged_by', type: 'uuid', nullable: true })
  acknowledgedBy: string;

  // Metadata
  @Column({ type: 'jsonb', default: {} })
  metadata: Record<string, any>;

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

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