import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { WhatsAppAccount } from './account.entity';
import { WhatsAppContact } from './contact.entity';

export type MessageType = 'text' | 'image' | 'video' | 'audio' | 'document' | 'sticker' | 'location' | 'contacts' | 'interactive' | 'template' | 'reaction';
export type MessageStatus = 'pending' | 'sent' | 'delivered' | 'read' | 'failed';
export type MessageDirection = 'inbound' | 'outbound';
export type CostCategory = 'utility' | 'authentication' | 'marketing';

@Entity({ name: 'messages', schema: 'whatsapp' })
export class WhatsAppMessage {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Index()
  @Column({ name: 'account_id', type: 'uuid' })
  accountId: string;

  @Index()
  @Column({ name: 'contact_id', type: 'uuid' })
  contactId: string;

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

  @Index()
  @Column({ name: 'wa_message_id', type: 'varchar', length: 100, nullable: true })
  waMessageId: string;

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

  @Index()
  @Column({ name: 'direction', type: 'varchar', length: 10 })
  direction: MessageDirection;

  @Column({ name: 'message_type', type: 'varchar', length: 20 })
  messageType: MessageType;

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

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

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

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

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

  @Column({ name: 'media_sha256', type: 'varchar', length: 64, nullable: true })
  mediaSha256: string;

  @Column({ name: 'media_size_bytes', type: 'int', nullable: true })
  mediaSizeBytes: number;

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

  @Column({ name: 'template_name', type: 'varchar', length: 512, nullable: true })
  templateName: string;

  @Column({ name: 'template_variables', type: 'jsonb', default: [] })
  templateVariables: string[];

  @Column({ name: 'interactive_type', type: 'varchar', length: 30, nullable: true })
  interactiveType: string;

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

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

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

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

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

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

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

  @Column({ name: 'is_billable', type: 'boolean', default: false })
  isBillable: boolean;

  @Column({ name: 'cost_category', type: 'varchar', length: 30, nullable: true })
  costCategory: CostCategory;

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

  @Index()
  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: 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;

  @ManyToOne(() => WhatsAppAccount, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'account_id' })
  account: WhatsAppAccount;

  @ManyToOne(() => WhatsAppContact, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'contact_id' })
  contact: WhatsAppContact;
}
