/**
 * Document Category Entity
 * Sistema de categorias jerarquicas para documentos.
 *
 * @module Documents (MAE-016)
 */

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

export type AccessLevel = 'public' | 'internal' | 'confidential' | 'restricted';

@Entity('document_categories', { schema: 'documents' })
@Index(['tenantId', 'code'], { unique: true })
@Index(['tenantId', 'parentId'])
export class DocumentCategory {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Informacion basica
  @Column({ length: 50 })
  code!: string;

  @Column({ length: 100 })
  name!: string;

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

  // Jerarquia
  @Column({ name: 'parent_id', type: 'uuid', nullable: true })
  parentId?: string;

  @ManyToOne(() => DocumentCategory, { nullable: true })
  @JoinColumn({ name: 'parent_id' })
  parent?: DocumentCategory;

  @OneToMany(() => DocumentCategory, (cat) => cat.parent)
  children?: DocumentCategory[];

  @Column({ type: 'int', default: 1 })
  level!: number;

  @Column({ length: 500, nullable: true })
  path?: string;

  // Configuracion
  @Column({
    name: 'default_access_level',
    type: 'enum',
    enum: ['public', 'internal', 'confidential', 'restricted'],
    enumName: 'access_level',
    default: 'internal',
  })
  defaultAccessLevel!: AccessLevel;

  @Column({ name: 'requires_approval', type: 'boolean', default: false })
  requiresApproval!: boolean;

  @Column({ name: 'retention_days', type: 'int', nullable: true })
  retentionDays?: number;

  @Column({ name: 'allowed_extensions', type: 'varchar', array: true, nullable: true })
  allowedExtensions?: string[];

  // Icono y color
  @Column({ length: 50, nullable: true })
  icon?: string;

  @Column({ length: 20, nullable: true })
  color?: string;

  // Estado
  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive!: boolean;

  @Column({ name: 'sort_order', type: 'int', default: 0 })
  sortOrder!: number;

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

  // Auditoria
  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdBy?: string;

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

  @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;
}
