import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { WhatsAppMessage } from './message.entity';

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

  @Index()
  @Column({ name: 'message_id', type: 'uuid' })
  messageId: string;

  @Column({ name: 'status', type: 'varchar', length: 20 })
  status: string;

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

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

  @Column({ name: 'error_title', type: 'varchar', length: 200, nullable: true })
  errorTitle: string;

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

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

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

  @ManyToOne(() => WhatsAppMessage, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'message_id' })
  message: WhatsAppMessage;
}
