/**
 * Document Share Entity
 * Links de compartido externo para documentos.
 *
 * @module Documents (MAE-016)
 */

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

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

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

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

  @ManyToOne(() => Document, { 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;

  // Link compartido
  @Column({ name: 'share_token', length: 100, unique: true })
  shareToken!: string;

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

  // Destinatario
  @Column({ name: 'shared_with_email', length: 255, nullable: true })
  sharedWithEmail?: string;

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

  // Permisos
  @Column({ name: 'can_download', type: 'boolean', default: false })
  canDownload!: boolean;

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

  // Seguridad
  @Column({ name: 'password_hash', length: 255, nullable: true })
  passwordHash?: string;

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

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

  // Vigencia
  @Column({ name: 'expires_at', type: 'timestamptz', nullable: true })
  expiresAt?: Date;

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

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

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

  // Compartido por
  @Column({ name: 'shared_by_id', type: 'uuid' })
  sharedById!: string;

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

  // Notificacion
  @Column({ name: 'notification_sent', type: 'boolean', default: false })
  notificationSent!: boolean;

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

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

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

  // FK a auth.users (ERP-Core) - usuario que compartió
  @ManyToOne(() => User, { nullable: false })
  @JoinColumn({ name: 'shared_by_id' })
  sharedBy!: User;

  // FK a auth.users (ERP-Core) - usuario que revocó (si aplica)
  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'revoked_by_id' })
  revokedBy?: User;
}
