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

/**
 * 消息角色常量
 */
export const MessageRole = {
  USER: 'user',
  ASSISTANT: 'assistant',
  SYSTEM: 'system',
} as const;

export type MessageRoleType = (typeof MessageRole)[keyof typeof MessageRole];

/**
 * 消息类型常量
 */
export const MessageType = {
  TEXT: 'TEXT',
  TEXT_WITH_ATTACHMENTS: 'TEXT_WITH_ATTACHMENTS',
  TOOL_CALL: 'TOOL_CALL',
  TOOL_RESULT: 'TOOL_RESULT',
  PAYMENT_REQUEST: 'PAYMENT_REQUEST',
  ASSESSMENT_START: 'ASSESSMENT_START',
  ASSESSMENT_RESULT: 'ASSESSMENT_RESULT',
} as const;

export type MessageTypeType = (typeof MessageType)[keyof typeof MessageType];

@Entity('messages')
export class MessageEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ length: 20 })
  role: MessageRoleType;

  @Column({ length: 30, default: 'TEXT' })
  type: MessageTypeType;

  @Column({ type: 'text' })
  content: string;

  @Column({ type: 'jsonb', nullable: true })
  metadata: Record<string, unknown>;

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

  @ManyToOne(() => ConversationEntity, (conversation) => conversation.messages)
  @JoinColumn({ name: 'conversation_id' })
  conversation: ConversationEntity;
}
