import {
  Column,
  CreateDateColumn,
  Entity,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
  ManyToMany,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { KnowledgeGroup } from '../knowledge-group/knowledge-group.entity';
import { Tenant } from '../tenant/tenant.entity';

export enum FileStatus {
  PENDING = 'pending',
  INDEXING = 'indexing',
  EXTRACTED = 'extracted', // Text extraction completed and saved to database
  VECTORIZED = 'vectorized', // Vectorization completed and indexed to ES
  FAILED = 'failed',
}

export enum ProcessingMode {
  FAST = 'fast',      // Fast mode - use Tika
  PRECISE = 'precise', // Precise mode - use Vision Pipeline
}

@Entity('knowledge_bases')
export class KnowledgeBase {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'original_name' })
  originalName: string;

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

  @Column({ name: 'storage_path' })
  storagePath: string;

  @Column({ type: 'integer', default: 0 })
  size: number;

  @Column({ length: 100, nullable: true })
  mimetype: string;

  @Column({
    type: 'simple-enum',
    enum: FileStatus,
    default: FileStatus.PENDING,
  })
  status: FileStatus;

  @Column({ name: 'user_id', nullable: true }) // Temporarily allowed empty (for debugging), should be required in future
  userId: string;

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

  @ManyToOne(() => Tenant, { nullable: true, onDelete: 'CASCADE' })
  @JoinColumn({ name: 'tenant_id' })
  tenant: Tenant;

  @Column({ type: 'text', nullable: true })
  content: string; // Stores text content extracted by Tika

  // Index setting parameters
  @Column({ name: 'chunk_size', type: 'integer', default: 1000 })
  chunkSize: number;

  @Column({ name: 'chunk_overlap', type: 'integer', default: 200 })
  chunkOverlap: number;

  @Column({ name: 'embedding_model_id', nullable: true })
  embeddingModelId: string;

  @Column({
    type: 'simple-enum',
    enum: ProcessingMode,
    default: ProcessingMode.FAST,
    name: 'processing_mode',
  })
  processingMode: ProcessingMode;

  @Column({ type: 'json', nullable: true })
  metadata: any; // Stores additional metadata (image descriptions, confidence, etc.)

  @Column({ name: 'pdf_path', nullable: true })
  pdfPath: string; // PDF file path (for preview)

  @ManyToMany(() => KnowledgeGroup, (group) => group.knowledgeBases)
  groups: KnowledgeGroup[];

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

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