import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { SearchHistory } from './search-history.entity';
import { Tenant } from '../tenant/tenant.entity';

@Entity('chat_messages')
export class ChatMessage {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'search_history_id' })
  searchHistoryId: string;

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

  @Column()
  role: 'user' | 'assistant';

  @Column('text')
  content: string;

  @Column({ nullable: true })
  sources?: string; // JSON string

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

  @ManyToOne(() => SearchHistory, (history) => history.messages, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'search_history_id' })
  searchHistory: SearchHistory;

  @ManyToOne(() => Tenant)
  @JoinColumn({ name: 'tenant_id' })
  tenant: Tenant;
}