/**
 * Port ORM Mapper
 *
 * Maps between Port domain entity and PortOrmEntity
 */

import { Port, PortProps } from '@domain/entities/port.entity';
import { PortOrmEntity } from '../entities/port.orm-entity';

export class PortOrmMapper {
  /**
   * Map domain entity to ORM entity
   */
  static toOrm(domain: Port): PortOrmEntity {
    const orm = new PortOrmEntity();
    const props = domain.toObject();

    orm.id = props.id;
    orm.code = props.code;
    orm.name = props.name;
    orm.city = props.city;
    orm.country = props.country;
    orm.countryName = props.countryName;
    orm.latitude = props.coordinates.latitude;
    orm.longitude = props.coordinates.longitude;
    orm.timezone = props.timezone || null;
    orm.isActive = props.isActive;
    orm.createdAt = props.createdAt;
    orm.updatedAt = props.updatedAt;

    return orm;
  }

  /**
   * Map ORM entity to domain entity
   */
  static toDomain(orm: PortOrmEntity): Port {
    const props: PortProps = {
      id: orm.id,
      code: orm.code,
      name: orm.name,
      city: orm.city,
      country: orm.country,
      countryName: orm.countryName,
      coordinates: {
        latitude: Number(orm.latitude),
        longitude: Number(orm.longitude),
      },
      timezone: orm.timezone || undefined,
      isActive: orm.isActive,
      createdAt: orm.createdAt,
      updatedAt: orm.updatedAt,
    };

    return Port.fromPersistence(props);
  }

  /**
   * Map array of ORM entities to domain entities
   */
  static toDomainMany(orms: PortOrmEntity[]): Port[] {
    return orms.map(orm => this.toDomain(orm));
  }
}
