import {
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  VersionColumn,
} from 'typeorm';

/**
 * Base entity with common fields for all domain entities.
 * Includes optimistic locking via @VersionColumn for concurrent access safety.
 *
 * All domain entities should extend this:
 *   @Entity('users')
 *   export class User extends BaseEntity { ... }
 */
export abstract class BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

  /**
   * Optimistic lock version.
   * TypeORM auto-increments this on every UPDATE.
   * If another transaction modified the row, save() throws OptimisticLockVersionMismatchError.
   */
  @VersionColumn({ default: 1 })
  version: number;
}
