import { ApiKey } from '@domain/entities/api-key.entity';
import { ApiKeyOrmEntity } from '../entities/api-key.orm-entity';

export class ApiKeyOrmMapper {
  static toDomain(orm: ApiKeyOrmEntity): ApiKey {
    return ApiKey.fromPersistence({
      id: orm.id,
      organizationId: orm.organizationId,
      userId: orm.userId,
      name: orm.name,
      keyHash: orm.keyHash,
      keyPrefix: orm.keyPrefix,
      isActive: orm.isActive,
      lastUsedAt: orm.lastUsedAt,
      expiresAt: orm.expiresAt,
      createdAt: orm.createdAt,
      updatedAt: orm.updatedAt,
    });
  }

  static toOrm(domain: ApiKey): ApiKeyOrmEntity {
    const orm = new ApiKeyOrmEntity();
    orm.id = domain.id;
    orm.organizationId = domain.organizationId;
    orm.userId = domain.userId;
    orm.name = domain.name;
    orm.keyHash = domain.keyHash;
    orm.keyPrefix = domain.keyPrefix;
    orm.isActive = domain.isActive;
    orm.lastUsedAt = domain.lastUsedAt;
    orm.expiresAt = domain.expiresAt;
    orm.createdAt = domain.createdAt;
    orm.updatedAt = domain.updatedAt;
    return orm;
  }

  static toDomainMany(orms: ApiKeyOrmEntity[]): ApiKey[] {
    return orms.map(orm => ApiKeyOrmMapper.toDomain(orm));
  }
}
