import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';
import { User } from './user.entity';

export enum ResponseStyle {
  CONCISE = 'concise',
  DETAILED = 'detailed',
  BALANCED = 'balanced',
}

export enum ResponseTone {
  FRIENDLY = 'friendly',
  PROFESSIONAL = 'professional',
  CASUAL = 'casual',
  EMPATHETIC = 'empathetic',
}

export interface TopicPreference {
  topic: string;
  weight: number; // 0-1, higher = more preferred
}

export interface ResponsePattern {
  pattern: string;
  count: number;
  lastSeen: Date;
}

@Entity('user_preferences')
export class UserPreferences {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id' })
  user: User;

  // AI Response Style Preferences
  @Column({
    name: 'response_style',
    type: 'varchar',
    length: 50,
    default: ResponseStyle.BALANCED,
  })
  responseStyle: ResponseStyle;

  @Column({
    name: 'tone',
    type: 'varchar',
    length: 50,
    default: ResponseTone.FRIENDLY,
  })
  tone: ResponseTone;

  @Column({
    name: 'language',
    type: 'varchar',
    length: 10,
    default: 'en',
  })
  language: string;

  // Content Preferences
  @Column({
    name: 'include_medical_disclaimers',
    type: 'boolean',
    default: true,
  })
  includeMedicalDisclaimers: boolean;

  @Column({
    name: 'include_research_references',
    type: 'boolean',
    default: false,
  })
  includeResearchReferences: boolean;

  @Column({
    name: 'focus_areas',
    type: 'text',
    array: true,
    default: () => "ARRAY[]::text[]",
  })
  focusAreas: string[];

  // Feedback-Based Learning
  @Column({
    name: 'preferred_topics',
    type: 'jsonb',
    default: () => "'[]'::jsonb",
  })
  preferredTopics: TopicPreference[];

  @Column({
    name: 'avoided_topics',
    type: 'jsonb',
    default: () => "'[]'::jsonb",
  })
  avoidedTopics: TopicPreference[];

  @Column({
    name: 'helpful_response_patterns',
    type: 'jsonb',
    default: () => "'[]'::jsonb",
  })
  helpfulResponsePatterns: ResponsePattern[];

  @Column({
    name: 'unhelpful_response_patterns',
    type: 'jsonb',
    default: () => "'[]'::jsonb",
  })
  unhelpfulResponsePatterns: ResponsePattern[];

  // Interaction Preferences
  @Column({
    name: 'max_response_length',
    type: 'integer',
    default: 500,
  })
  maxResponseLength: number;

  @Column({
    name: 'prefer_bullet_points',
    type: 'boolean',
    default: false,
  })
  preferBulletPoints: boolean;

  @Column({
    name: 'prefer_examples',
    type: 'boolean',
    default: true,
  })
  preferExamples: boolean;

  @Column({
    name: 'prefer_step_by_step',
    type: 'boolean',
    default: false,
  })
  preferStepByStep: boolean;

  // Privacy & Safety
  @Column({
    name: 'share_data_for_improvement',
    type: 'boolean',
    default: true,
  })
  shareDataForImprovement: boolean;

  @Column({
    name: 'allow_personalization',
    type: 'boolean',
    default: true,
  })
  allowPersonalization: boolean;

  // Metadata
  @Column({
    name: 'total_interactions',
    type: 'integer',
    default: 0,
  })
  totalInteractions: number;

  @Column({
    name: 'positive_feedback_count',
    type: 'integer',
    default: 0,
  })
  positiveFeedbackCount: number;

  @Column({
    name: 'negative_feedback_count',
    type: 'integer',
    default: 0,
  })
  negativeFeedbackCount: number;

  @Column({
    name: 'last_updated_from_feedback',
    type: 'timestamp',
    nullable: true,
  })
  lastUpdatedFromFeedback: Date;

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

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

  // Helper methods
  getFeedbackRatio(): number {
    const total =
      this.positiveFeedbackCount + this.negativeFeedbackCount;
    if (total === 0) return 0.5; // Neutral
    return this.positiveFeedbackCount / total;
  }

  isPersonalizationEnabled(): boolean {
    return this.allowPersonalization && this.shareDataForImprovement;
  }

  getPreferredTopicWeight(topic: string): number {
    const pref = this.preferredTopics.find((t) => t.topic === topic);
    return pref ? pref.weight : 0.5; // Default neutral weight
  }

  isTopicAvoided(topic: string): boolean {
    return this.avoidedTopics.some((t) => t.topic === topic);
  }
}
