import { Entity, Column, PrimaryColumn, ManyToOne, JoinColumn } from 'typeorm';
import { User } from './user.entity';
import { Child } from './child.entity';
import { Activity } from './activity.entity';

export enum VoiceFeedbackAction {
  APPROVED = 'approved',
  EDITED = 'edited',
  REJECTED = 'rejected',
}

@Entity('voice_feedback')
export class VoiceFeedback {
  @PrimaryColumn({ type: 'varchar', length: 20 })
  id: string;

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

  @Column({ name: 'child_id', type: 'varchar', length: 20, nullable: true })
  childId?: string;

  @Column({ name: 'activity_id', type: 'varchar', length: 20, nullable: true })
  activityId?: string;

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

  @Column({ type: 'varchar', length: 10, nullable: true })
  language?: string;

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

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

  @Column({ type: 'decimal', precision: 3, scale: 2, nullable: true })
  confidence?: number;

  @Column({
    type: 'varchar',
    length: 20,
    enum: VoiceFeedbackAction,
  })
  action: VoiceFeedbackAction;

  @Column({ name: 'final_type', type: 'varchar', length: 20, nullable: true })
  finalType?: string;

  @Column({ name: 'final_data', type: 'jsonb', nullable: true })
  finalData?: Record<string, any>;

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

  @Column({
    name: 'created_at',
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
  })
  createdAt: Date;

  // Relations
  @ManyToOne(() => User, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'user_id' })
  user: User;

  @ManyToOne(() => Child, { nullable: true, onDelete: 'CASCADE' })
  @JoinColumn({ name: 'child_id' })
  child?: Child;

  @ManyToOne(() => Activity, { nullable: true, onDelete: 'CASCADE' })
  @JoinColumn({ name: 'activity_id' })
  activity?: Activity;
}
