/**
 * Approval Action Entity
 * Acciones tomadas en pasos de aprobacion.
 *
 * @module Documents (MAE-016)
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { ApprovalStep } from './approval-step.entity';
import { ApprovalInstance, ApprovalAction } from './approval-instance.entity';

@Entity('approval_actions', { schema: 'documents' })
@Index(['stepId'])
@Index(['userId'])
export class ApprovalActionEntity {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Paso
  @Column({ name: 'step_id', type: 'uuid' })
  stepId!: string;

  @ManyToOne(() => ApprovalStep, (step) => step.actions, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'step_id' })
  step!: ApprovalStep;

  @Column({ name: 'instance_id', type: 'uuid' })
  instanceId!: string;

  @ManyToOne(() => ApprovalInstance)
  @JoinColumn({ name: 'instance_id' })
  instance!: ApprovalInstance;

  // Accion
  @Column({
    type: 'enum',
    enum: ['approve', 'reject', 'request_changes'],
    enumName: 'approval_action',
  })
  action!: ApprovalAction;

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

  // Usuario que toma la accion
  @Column({ name: 'user_id', type: 'uuid' })
  userId!: string;

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

  // Firma digital (si aplica)
  @Column({ name: 'signature_data', type: 'text', nullable: true })
  signatureData?: string;

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

  @Column({ name: 'signature_ip', length: 45, nullable: true })
  signatureIp?: string;

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