/**
 * InvitationToken ORM Mapper
 *
 * Maps between domain InvitationToken entity and TypeORM InvitationTokenOrmEntity
 */

import { InvitationToken } from '@domain/entities/invitation-token.entity';
import { UserRole } from '@domain/entities/user.entity';
import { InvitationTokenOrmEntity } from '../entities/invitation-token.orm-entity';

export class InvitationTokenOrmMapper {
  /**
   * Map from ORM entity to domain entity
   */
  static toDomain(ormEntity: InvitationTokenOrmEntity): InvitationToken {
    return InvitationToken.fromPersistence({
      id: ormEntity.id,
      token: ormEntity.token,
      email: ormEntity.email,
      firstName: ormEntity.firstName,
      lastName: ormEntity.lastName,
      role: ormEntity.role as UserRole,
      organizationId: ormEntity.organizationId,
      invitedById: ormEntity.invitedById,
      expiresAt: ormEntity.expiresAt,
      usedAt: ormEntity.usedAt || undefined,
      isUsed: ormEntity.isUsed,
      createdAt: ormEntity.createdAt,
    });
  }

  /**
   * Map from domain entity to ORM entity
   */
  static toOrm(domain: InvitationToken): InvitationTokenOrmEntity {
    const ormEntity = new InvitationTokenOrmEntity();

    ormEntity.id = domain.id;
    ormEntity.token = domain.token;
    ormEntity.email = domain.email;
    ormEntity.firstName = domain.firstName;
    ormEntity.lastName = domain.lastName;
    ormEntity.role = domain.role;
    ormEntity.organizationId = domain.organizationId;
    ormEntity.invitedById = domain.invitedById;
    ormEntity.expiresAt = domain.expiresAt;
    ormEntity.usedAt = domain.usedAt || null;
    ormEntity.isUsed = domain.isUsed;
    ormEntity.createdAt = domain.createdAt;

    return ormEntity;
  }
}
