import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
} from 'typeorm';

export enum FeedbackType {
  BUG_REPORT = 'bug_report',
  FEATURE_REQUEST = 'feature_request',
  GENERAL_FEEDBACK = 'general_feedback',
  CONTENT_ISSUE = 'content_issue',
  USABILITY_ISSUE = 'usability_issue',
  PERFORMANCE_ISSUE = 'performance_issue',
}

export enum FeedbackStatus {
  NEW = 'new',
  TRIAGED = 'triaged',
  IN_PROGRESS = 'in_progress',
  RESOLVED = 'resolved',
  CLOSED = 'closed',
  WONT_FIX = 'wont_fix',
}

export enum FeedbackPriority {
  LOW = 'low',
  MEDIUM = 'medium',
  HIGH = 'high',
  CRITICAL = 'critical',
}

export enum FeedbackSentiment {
  VERY_NEGATIVE = 'very_negative',
  NEGATIVE = 'negative',
  NEUTRAL = 'neutral',
  POSITIVE = 'positive',
  VERY_POSITIVE = 'very_positive',
}

@Entity('feedback')
@Index(['userId', 'createdAt'])
@Index(['type', 'status'])
@Index(['priority', 'status'])
export class Feedback {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  @Index()
  userId: string;

  @Column({
    type: 'enum',
    enum: FeedbackType,
  })
  type: FeedbackType;

  @Column({
    type: 'enum',
    enum: FeedbackStatus,
    default: FeedbackStatus.NEW,
  })
  status: FeedbackStatus;

  @Column({
    type: 'enum',
    enum: FeedbackPriority,
    default: FeedbackPriority.MEDIUM,
  })
  priority: FeedbackPriority;

  @Column({
    type: 'enum',
    enum: FeedbackSentiment,
    nullable: true,
  })
  sentiment: FeedbackSentiment;

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

  @Column({ nullable: true })
  title: string;

  @Column({ nullable: true })
  category: string; // e.g., 'tracking', 'ai', 'family', 'analytics'

  @Column({ nullable: true })
  page: string; // Which page/screen the feedback was submitted from

  @Column({ nullable: true })
  platform: string; // 'web', 'ios', 'android'

  @Column({ nullable: true })
  appVersion: string;

  @Column({ nullable: true })
  browserInfo: string;

  @Column({ nullable: true })
  deviceInfo: string;

  @Column({ type: 'jsonb', nullable: true })
  metadata: Record<string, any>; // Additional context (screenshots, logs, etc.)

  @Column({ type: 'jsonb', nullable: true })
  tags: string[]; // Searchable tags

  @Column({ type: 'int', default: 0 })
  upvotes: number; // For feature requests

  @Column({ nullable: true })
  userEmail: string; // For follow-up

  @Column({ default: false })
  contactAllowed: boolean; // User allows contact for follow-up

  @Column({ type: 'text', nullable: true })
  internalNotes: string; // Internal team notes

  @Column({ nullable: true })
  assignedTo: string; // Team member assigned to handle this

  @Column({ type: 'timestamp', nullable: true })
  resolvedAt: Date;

  @Column({ nullable: true })
  resolvedBy: string;

  @Column({ type: 'text', nullable: true })
  resolution: string; // How it was resolved

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}
