import {
  Column,
  Entity,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  Unique,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Conversation } from './conversation.entity';

@Entity('messages')
@Unique(['time_raw', 'room_id'])
export class Message {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ nullable: true })
  name: string; // tên người gửi

  @Column({ type: 'json', nullable: true })
  message: string; // nội dung

  @Column({ type: 'timestamp' })
  time: Date;

  @Column({ type: 'bigint' })
  time_raw: number;

  @Column({ type: 'bigint' })
  date_time: number;

  @Column({ nullable: true })
  room_id: string;

  @Column({ nullable: true })
  room_name: string;

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;

  @ManyToOne(() => Conversation, (group) => group.messages, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'room_id', referencedColumnName: 'id' })
  conversation: Conversation;
}
