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

export type ChangeType = 'create' | 'update' | 'delete' | 'restore';

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

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

  @Index()
  @Column({ name: 'entity_type', type: 'varchar', length: 100 })
  entityType: string;

  @Index()
  @Column({ name: 'entity_id', type: 'uuid' })
  entityId: string;

  @Column({ name: 'entity_name', type: 'varchar', length: 255, nullable: true })
  entityName: string;

  @Column({ name: 'version', type: 'int', default: 1 })
  version: number;

  @Column({ name: 'previous_version', type: 'int', nullable: true })
  previousVersion: number;

  @Column({ name: 'data_snapshot', type: 'jsonb' })
  dataSnapshot: Record<string, any>;

  @Column({ name: 'changes', type: 'jsonb', default: [] })
  changes: Record<string, any>[];

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

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

  @Column({ name: 'change_type', type: 'varchar', length: 20 })
  changeType: ChangeType;

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