/**
 * Annotation Entity
 * Anotaciones y comentarios sobre documentos.
 *
 * @module Documents (MAE-016)
 */

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

export type AnnotationType =
  | 'comment'
  | 'markup'
  | 'highlight'
  | 'arrow'
  | 'dimension'
  | 'stamp'
  | 'signature';

@Entity('annotations', { schema: 'documents' })
@Index(['tenantId'])
@Index(['documentId'])
@Index(['versionId'])
@Index(['authorId'])
export class Annotation {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

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

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

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

  @ManyToOne(() => DocumentVersion, { nullable: true })
  @JoinColumn({ name: 'version_id' })
  version?: DocumentVersion;

  // Tipo
  @Column({
    name: 'annotation_type',
    type: 'enum',
    enum: ['comment', 'markup', 'highlight', 'arrow', 'dimension', 'stamp', 'signature'],
    enumName: 'annotation_type',
  })
  annotationType!: AnnotationType;

  // Ubicacion en el documento
  @Column({ name: 'page_number', type: 'int', nullable: true })
  pageNumber?: number;

  @Column({ name: 'x_position', type: 'decimal', precision: 10, scale: 4, nullable: true })
  xPosition?: number;

  @Column({ name: 'y_position', type: 'decimal', precision: 10, scale: 4, nullable: true })
  yPosition?: number;

  @Column({ type: 'decimal', precision: 10, scale: 4, nullable: true })
  width?: number;

  @Column({ type: 'decimal', precision: 10, scale: 4, nullable: true })
  height?: number;

  @Column({ type: 'decimal', precision: 5, scale: 2, nullable: true })
  rotation?: number;

  // Contenido
  @Column({ type: 'text', nullable: true })
  content?: string;

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

  // Respuesta a otra anotacion
  @Column({ name: 'parent_annotation_id', type: 'uuid', nullable: true })
  parentAnnotationId?: string;

  @ManyToOne(() => Annotation, { nullable: true })
  @JoinColumn({ name: 'parent_annotation_id' })
  parentAnnotation?: Annotation;

  // Estado
  @Column({ name: 'is_resolved', type: 'boolean', default: false })
  isResolved!: boolean;

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

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

  // Visibilidad
  @Column({ name: 'is_private', type: 'boolean', default: false })
  isPrivate!: boolean;

  @Column({ name: 'visible_to', type: 'uuid', array: true, nullable: true })
  visibleTo?: string[];

  // Autor
  @Column({ name: 'author_id', type: 'uuid' })
  authorId!: string;

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

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

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt!: Date;

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