import { RatingEvent } from '@core/identity/domain/entities/RatingEvent';
import { RatingEventId } from '@core/identity/domain/value-objects/RatingEventId';
import { RatingDimensionKey } from '@core/identity/domain/value-objects/RatingDimensionKey';
import { RatingDelta } from '@core/identity/domain/value-objects/RatingDelta';
import { RatingEventOrmEntity } from '../entities/RatingEventOrmEntity';

/**
 * Mapper: RatingEventOrmMapper
 * 
 * Converts between RatingEvent domain entity and RatingEventOrmEntity.
 */
export class RatingEventOrmMapper {
  /**
   * Convert ORM entity to domain entity
   */
  static toDomain(entity: RatingEventOrmEntity): RatingEvent {
    const props: any = {
      id: RatingEventId.create(entity.id),
      userId: entity.userId,
      dimension: RatingDimensionKey.create(entity.dimension),
      delta: RatingDelta.create(entity.delta),
      occurredAt: entity.occurredAt,
      createdAt: entity.createdAt,
      source: entity.source,
      reason: entity.reason,
      visibility: entity.visibility,
      version: entity.version,
    };
    
    if (entity.weight !== undefined && entity.weight !== null) {
      props.weight = entity.weight;
    }
    
    return RatingEvent.rehydrate(props);
  }

  /**
   * Convert domain entity to ORM entity
   */
  static toOrmEntity(domain: RatingEvent): RatingEventOrmEntity {
    const entity = new RatingEventOrmEntity();
    entity.id = domain.id.value;
    entity.userId = domain.userId;
    entity.dimension = domain.dimension.value;
    entity.delta = domain.delta.value;
    if (domain.weight !== undefined) {
      entity.weight = domain.weight;
    }
    entity.occurredAt = domain.occurredAt;
    entity.createdAt = domain.createdAt;
    entity.source = domain.source;
    entity.reason = domain.reason;
    entity.visibility = domain.visibility;
    entity.version = domain.version;
    return entity;
  }
}