/**
 * Document Version Entity
 * Control de versiones de documentos con archivos.
 *
 * @module Documents (MAE-016)
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Document } from './document.entity';

export type VersionStatus = 'current' | 'superseded' | 'archived';

@Entity('document_versions', { schema: 'documents' })
@Index(['tenantId'])
@Index(['documentId'])
@Index(['status'])
export class DocumentVersion {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Documento padre
  @Column({ name: 'document_id', type: 'uuid' })
  documentId!: string;

  @ManyToOne(() => Document, (doc) => doc.versions, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'document_id' })
  document!: Document;

  // Version
  @Column({ name: 'version_number', length: 20 })
  versionNumber!: string;

  @Column({ name: 'version_label', length: 100, nullable: true })
  versionLabel?: string;

  @Column({
    type: 'enum',
    enum: ['current', 'superseded', 'archived'],
    enumName: 'version_status',
    default: 'current',
  })
  status!: VersionStatus;

  // Archivo
  @Column({ name: 'file_name', length: 500 })
  fileName!: string;

  @Column({ name: 'file_extension', length: 20 })
  fileExtension!: string;

  @Column({ name: 'file_size_bytes', type: 'bigint' })
  fileSizeBytes!: number;

  @Column({ name: 'mime_type', length: 100, nullable: true })
  mimeType?: string;

  @Column({ name: 'checksum_md5', length: 32, nullable: true })
  checksumMd5?: string;

  @Column({ name: 'checksum_sha256', length: 64, nullable: true })
  checksumSha256?: string;

  // Almacenamiento
  @Column({ name: 'storage_provider', length: 50, default: 's3' })
  storageProvider!: string;

  @Column({ name: 'storage_bucket', length: 255, nullable: true })
  storageBucket?: string;

  @Column({ name: 'storage_key', length: 1000 })
  storageKey!: string;

  @Column({ name: 'storage_url', length: 2000, nullable: true })
  storageUrl?: string;

  @Column({ name: 'thumbnail_url', length: 2000, nullable: true })
  thumbnailUrl?: string;

  @Column({ name: 'preview_url', length: 2000, nullable: true })
  previewUrl?: string;

  // Procesamiento
  @Column({ name: 'is_processed', type: 'boolean', default: false })
  isProcessed!: boolean;

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

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

  // Paginas
  @Column({ name: 'page_count', type: 'int', nullable: true })
  pageCount?: number;

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

  // Cambios
  @Column({ name: 'change_summary', type: 'text', nullable: true })
  changeSummary?: string;

  @Column({ name: 'change_type', length: 50, nullable: true })
  changeType?: string;

  // Subido por
  @Column({ name: 'uploaded_by_id', type: 'uuid', nullable: true })
  uploadedById?: string;

  @Column({ name: 'uploaded_by_name', length: 255, nullable: true })
  uploadedByName?: string;

  @Column({ name: 'upload_source', length: 50, nullable: true })
  uploadSource?: string;

  // Auditoria
  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt!: Date;

  @Column({ name: 'superseded_at', type: 'timestamptz', nullable: true })
  supersededAt?: Date;

  @Column({ name: 'superseded_by_version_id', type: 'uuid', nullable: true })
  supersededByVersionId?: string;
}
