import { Company } from '@core/identity/domain/entities/Company';
import { CompanyOrmEntity } from '../entities/CompanyOrmEntity';

export class CompanyOrmMapper {
  toDomain(entity: CompanyOrmEntity): Company {
    const contactEmail = entity.contactEmail ?? undefined;
    return Company.rehydrate({
      id: entity.id,
      name: entity.name,
      ownerUserId: entity.ownerUserId,
      ...(contactEmail !== undefined ? { contactEmail } : {}),
      createdAt: entity.createdAt,
    });
  }

  toPersistence(domain: Company): CompanyOrmEntity {
    const entity = new CompanyOrmEntity();
    entity.id = domain.getId();
    entity.name = domain.getName();
    entity.ownerUserId = domain.getOwnerUserId().toString();
    entity.contactEmail = domain.getContactEmail() ?? null;
    // Note: createdAt is handled by @CreateDateColumn
    return entity;
  }
}