/**
 * ConfigChange Entity
 * System configuration change auditing
 * Compatible with erp-core config-change.entity
 *
 * @module Audit
 */

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

export type ConfigType = 'tenant_settings' | 'user_settings' | 'system_settings' | 'feature_flags';

@Entity({ name: 'config_changes', schema: 'audit' })
export class ConfigChange {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'changed_by', type: 'uuid' })
  changedBy: string;

  @Index()
  @Column({ name: 'config_type', type: 'varchar', length: 50 })
  configType: ConfigType;

  @Column({ name: 'config_key', type: 'varchar', length: 100 })
  configKey: string;

  @Column({ name: 'config_path', type: 'text', nullable: true })
  configPath: string;

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

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

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

  @Column({ name: 'ticket_id', type: 'varchar', length: 50, nullable: true })
  ticketId: string;

  @Index()
  @Column({ name: 'changed_at', type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' })
  changedAt: Date;
}
