import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { ChannelType, Channel } from './channel.entity';
import { NotificationTemplate } from './template.entity';

export type NotificationStatus = 'pending' | 'queued' | 'sending' | 'sent' | 'delivered' | 'read' | 'failed' | 'cancelled';
export type NotificationPriority = 'low' | 'normal' | 'high' | 'urgent';

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

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

  @Index()
  @Column({ name: 'user_id', type: 'uuid', nullable: true })
  userId: string;

  @Column({ name: 'recipient_email', type: 'varchar', length: 255, nullable: true })
  recipientEmail: string;

  @Column({ name: 'recipient_phone', type: 'varchar', length: 20, nullable: true })
  recipientPhone: string;

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

  @Index()
  @Column({ name: 'template_id', type: 'uuid', nullable: true })
  templateId: string;

  @Column({ name: 'template_code', type: 'varchar', length: 100, nullable: true })
  templateCode: string;

  @Index()
  @Column({ name: 'channel_type', type: 'varchar', length: 30 })
  channelType: ChannelType;

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

  @Column({ name: 'subject', type: 'varchar', length: 500, nullable: true })
  subject: string;

  @Column({ name: 'body', type: 'text' })
  body: string;

  @Column({ name: 'body_html', type: 'text', nullable: true })
  bodyHtml: string;

  @Column({ name: 'variables', type: 'jsonb', default: {} })
  variables: Record<string, any>;

  @Index()
  @Column({ name: 'context_type', type: 'varchar', length: 50, nullable: true })
  contextType: string;

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

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

  @Index()
  @Column({ name: 'status', type: 'varchar', length: 20, default: 'pending' })
  status: NotificationStatus;

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

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

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

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

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

  @Column({ name: 'error_message', type: 'text', nullable: true })
  errorMessage: string;

  @Column({ name: 'retry_count', type: 'int', default: 0 })
  retryCount: number;

  @Column({ name: 'max_retries', type: 'int', default: 3 })
  maxRetries: number;

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

  @Column({ name: 'provider_message_id', type: 'varchar', length: 255, nullable: true })
  providerMessageId: string;

  @Column({ name: 'provider_response', type: 'jsonb', default: {} })
  providerResponse: Record<string, any>;

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

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

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

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

  @ManyToOne(() => NotificationTemplate, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'template_id' })
  template: NotificationTemplate;

  @ManyToOne(() => Channel, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'channel_id' })
  channel: Channel;
}
