import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
  Index,
} from 'typeorm';
import { MessageORM } from './message.orm';
import {
  ConversationStatusType,
  ConsultingStageType,
  ConsultingStateJson,
  DeviceInfo,
} from '../../../../domain/entities/conversation.entity';

/**
 * Conversation ORM Entity - Database representation
 */
@Entity('conversations')
@Index('idx_conversations_tenant', ['tenantId'])
@Index('idx_conversations_tenant_user', ['tenantId', 'userId'])
@Index('idx_conversations_tenant_status', ['tenantId', 'status'])
export class ConversationORM {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

  @Column({ name: 'user_id', type: 'uuid', nullable: true })
  userId: string;

  @Column({ type: 'varchar', length: 20, default: 'ACTIVE' })
  status: ConversationStatusType;

  @Column({ type: 'varchar', length: 255, nullable: true })
  title: string | null;

  @Column({ type: 'text', nullable: true })
  summary: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true })
  category: string | null;

  @Column({ name: 'message_count', type: 'int', default: 0 })
  messageCount: number;

  @Column({ name: 'user_message_count', type: 'int', default: 0 })
  userMessageCount: number;

  @Column({ name: 'assistant_message_count', type: 'int', default: 0 })
  assistantMessageCount: number;

  @Column({ name: 'total_input_tokens', type: 'int', default: 0 })
  totalInputTokens: number;

  @Column({ name: 'total_output_tokens', type: 'int', default: 0 })
  totalOutputTokens: number;

  @Column({ type: 'smallint', nullable: true })
  rating: number | null;

  @Column({ type: 'text', nullable: true })
  feedback: string | null;

  @Column({ name: 'has_converted', type: 'boolean', default: false })
  hasConverted: boolean;

  @Column({
    name: 'consulting_stage',
    type: 'varchar',
    length: 30,
    default: 'greeting',
    nullable: true,
  })
  consultingStage: ConsultingStageType | null;

  @Column({
    name: 'consulting_state',
    type: 'jsonb',
    nullable: true,
  })
  consultingState: ConsultingStateJson | null;

  @Column({
    name: 'collected_info',
    type: 'jsonb',
    nullable: true,
  })
  collectedInfo: Record<string, unknown> | null;

  @Column({
    name: 'recommended_programs',
    type: 'text',
    array: true,
    nullable: true,
  })
  recommendedPrograms: string[] | null;

  @Column({
    name: 'conversion_path',
    type: 'varchar',
    length: 30,
    nullable: true,
  })
  conversionPath: string | null;

  @Column({
    name: 'device_info',
    type: 'jsonb',
    nullable: true,
  })
  deviceInfo: DeviceInfo | null;

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

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;

  @Column({ name: 'ended_at', type: 'timestamptz', nullable: true })
  endedAt: Date | null;

  @OneToMany(() => MessageORM, (message) => message.conversation)
  messages: MessageORM[];
}
