import { TeamRatingEvent } from '@core/racing/domain/entities/TeamRatingEvent';
import { TeamRatingEventId } from '@core/racing/domain/value-objects/TeamRatingEventId';
import { TeamRatingDimensionKey } from '@core/racing/domain/value-objects/TeamRatingDimensionKey';
import { TeamRatingDelta } from '@core/racing/domain/value-objects/TeamRatingDelta';
import { TeamRatingEventOrmEntity } from '../entities/TeamRatingEventOrmEntity';

/**
 * Mapper: TeamRatingEventOrmMapper
 * 
 * Converts between TeamRatingEvent domain entity and TeamRatingEventOrmEntity.
 */
export class TeamRatingEventOrmMapper {
  /**
   * Convert ORM entity to domain entity
   */
  static toDomain(entity: TeamRatingEventOrmEntity): TeamRatingEvent {
    const props: any = {
      id: TeamRatingEventId.create(entity.id),
      teamId: entity.teamId,
      dimension: TeamRatingDimensionKey.create(entity.dimension),
      delta: TeamRatingDelta.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 TeamRatingEvent.rehydrate(props);
  }

  /**
   * Convert domain entity to ORM entity
   */
  static toOrmEntity(domain: TeamRatingEvent): TeamRatingEventOrmEntity {
    const entity = new TeamRatingEventOrmEntity();
    entity.id = domain.id.value;
    entity.teamId = domain.teamId;
    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;
  }
}