import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  Index,
} from 'typeorm';

/**
 * SecurityAuditLog - Audit trail for security IP log access
 *
 * Records every query to security_ip_log for compliance and security:
 * - Who accessed the data (admin ID)
 * - What action was performed (query type)
 * - Query parameters (searched IP, date range, etc.)
 * - When the access occurred
 *
 * This data is retained indefinitely as part of the audit trail.
 */
@Entity('security_audit_log')
export class SecurityAuditLog {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  /**
   * Type of access action performed.
   * Examples: SECURITY_IP_LOG_QUERY_BY_IP, SECURITY_IP_LOG_QUERY_BY_SESSION
   */
  @Column({ type: 'varchar', length: 100 })
  @Index('idx_security_audit_log_action')
  action!: string;

  /**
   * Admin user who performed the query.
   */
  @Column({ type: 'varchar', length: 255, name: 'admin_id' })
  @Index('idx_security_audit_log_admin')
  adminId!: string;

  /**
   * Admin email for easier identification in audit reports.
   */
  @Column({ type: 'varchar', length: 255, name: 'admin_email', nullable: true })
  adminEmail!: string | null;

  /**
   * Query details (parameters, filters, etc.)
   */
  @Column({ type: 'jsonb' })
  details!: Record<string, unknown>;

  /**
   * When the access occurred.
   */
  @Column({ type: 'timestamptz' })
  @Index('idx_security_audit_log_timestamp')
  timestamp!: Date;

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