/**
 * DigitalSignature Entity
 *
 * Firmas digitales para documentos con soporte para
 * firma simple, avanzada y cualificada.
 *
 * @module Documents (MAE-016)
 * @table documents.digital_signatures
 * @gap GAP-006
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';

export type SignatureType = 'simple' | 'advanced' | 'qualified';

export type SignatureStatus = 'pending' | 'signed' | 'rejected' | 'expired' | 'revoked';

@Entity({ schema: 'documents', name: 'digital_signatures' })
@Index(['tenantId'])
@Index(['documentId'])
@Index(['signerId'])
@Index(['tenantId', 'status'])
@Index(['signedAt'])
export class DigitalSignature {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

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

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

  // Firmante
  @Column({ name: 'signer_id', type: 'uuid' })
  signerId!: string;

  @Column({ name: 'signer_name', length: 200 })
  signerName!: string;

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

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

  // Tipo y estado
  @Column({
    name: 'signature_type',
    type: 'varchar',
    length: 20,
    default: 'simple',
  })
  signatureType!: SignatureType;

  @Column({
    type: 'varchar',
    length: 20,
    default: 'pending',
  })
  status!: SignatureStatus;

  // Datos de firma
  @Column({ name: 'signature_data', type: 'text', nullable: true })
  signatureData?: string;

  @Column({ name: 'signature_hash', length: 128, nullable: true })
  signatureHash?: string;

  @Column({ name: 'hash_algorithm', length: 20, default: 'SHA-256' })
  hashAlgorithm!: string;

  // Certificado
  @Column({ name: 'certificate_info', type: 'jsonb', nullable: true })
  certificateInfo?: Record<string, any>;

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

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

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

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

  // Timestamps
  @Column({ name: 'requested_at', type: 'timestamptz', default: () => 'NOW()' })
  requestedAt!: Date;

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

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

  // Contexto
  @Column({ name: 'ip_address', type: 'inet', nullable: true })
  ipAddress?: string;

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

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

  // Validación
  @Column({ name: 'is_valid', type: 'boolean', default: true })
  isValid!: boolean;

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

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

  // Razón
  @Column({ type: 'text', nullable: true })
  reason?: string;

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

  // Relaciones
  @ManyToOne(() => Tenant)
  @JoinColumn({ name: 'tenant_id' })
  tenant?: Tenant;

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

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