/**
 * Organization ORM Mapper
 *
 * Maps between Organization domain entity and OrganizationOrmEntity
 */

import { Organization, OrganizationProps } from '@domain/entities/organization.entity';
import { toLocale } from '@domain/value-objects/locale.vo';
import { OrganizationOrmEntity } from '../entities/organization.orm-entity';

export class OrganizationOrmMapper {
  /**
   * Map domain entity to ORM entity
   */
  static toOrm(domain: Organization): OrganizationOrmEntity {
    const orm = new OrganizationOrmEntity();
    const props = domain.toObject();

    orm.id = props.id;
    orm.name = props.name;
    orm.type = props.type;
    orm.scac = props.scac || null;
    orm.siren = props.siren || null;
    orm.eori = props.eori || null;
    orm.contactPhone = props.contact_phone || null;
    orm.contactEmail = props.contact_email || null;
    orm.addressStreet = props.address.street;
    orm.addressCity = props.address.city;
    orm.addressState = props.address.state || null;
    orm.addressPostalCode = props.address.postalCode;
    orm.addressCountry = props.address.country;
    orm.logoUrl = props.logoUrl || null;
    orm.documents = props.documents;
    orm.siret = props.siret || null;
    orm.siretVerified = props.siretVerified;
    orm.statusBadge = props.statusBadge;
    orm.isActive = props.isActive;
    orm.defaultLanguage = props.defaultLanguage;
    orm.createdAt = props.createdAt;
    orm.updatedAt = props.updatedAt;

    return orm;
  }

  /**
   * Map ORM entity to domain entity
   */
  static toDomain(orm: OrganizationOrmEntity): Organization {
    const props: OrganizationProps = {
      id: orm.id,
      name: orm.name,
      type: orm.type as any,
      scac: orm.scac || undefined,
      siren: orm.siren || undefined,
      eori: orm.eori || undefined,
      contact_phone: orm.contactPhone || undefined,
      contact_email: orm.contactEmail || undefined,
      address: {
        street: orm.addressStreet,
        city: orm.addressCity,
        state: orm.addressState || undefined,
        postalCode: orm.addressPostalCode,
        country: orm.addressCountry,
      },
      logoUrl: orm.logoUrl || undefined,
      documents: orm.documents || [],
      siret: orm.siret || undefined,
      siretVerified: orm.siretVerified ?? false,
      statusBadge: (orm.statusBadge as 'none' | 'silver' | 'gold' | 'platinium') || 'none',
      isActive: orm.isActive,
      defaultLanguage: toLocale(orm.defaultLanguage),
      createdAt: orm.createdAt,
      updatedAt: orm.updatedAt,
    };

    return Organization.fromPersistence(props);
  }

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