import type { MembershipFee } from '@core/payments/domain/entities/MembershipFee';
import { MembershipFee as MembershipFeeFactory, MembershipFeeType } from '@core/payments/domain/entities/MembershipFee';

import { PaymentsMembershipFeeOrmEntity } from '../entities/PaymentsMembershipFeeOrmEntity';
import {
    assertBoolean,
    assertDate,
    assertEnumValue,
    assertNonEmptyString,
    assertNumber,
    assertOptionalStringOrNull,
} from '../schema/TypeOrmPaymentsSchemaGuards';

export class PaymentsMembershipFeeOrmMapper {
  toOrmEntity(domain: MembershipFee): PaymentsMembershipFeeOrmEntity {
    const entity = new PaymentsMembershipFeeOrmEntity();
    entity.id = domain.id;
    entity.leagueId = domain.leagueId;
    entity.seasonId = domain.seasonId ?? null;
    entity.type = domain.type;
    entity.amount = domain.amount;
    entity.enabled = domain.enabled;
    entity.createdAt = domain.createdAt;
    entity.updatedAt = domain.updatedAt;
    return entity;
  }

  toDomain(entity: PaymentsMembershipFeeOrmEntity): MembershipFee {
    const entityName = 'PaymentsMembershipFee';

    assertNonEmptyString(entityName, 'id', entity.id);
    assertNonEmptyString(entityName, 'leagueId', entity.leagueId);
    assertOptionalStringOrNull(entityName, 'seasonId', entity.seasonId);
    assertEnumValue(entityName, 'type', entity.type, Object.values(MembershipFeeType));
    assertNumber(entityName, 'amount', entity.amount);
    assertBoolean(entityName, 'enabled', entity.enabled);
    assertDate(entityName, 'createdAt', entity.createdAt);
    assertDate(entityName, 'updatedAt', entity.updatedAt);

    return MembershipFeeFactory.rehydrate({
      id: entity.id,
      leagueId: entity.leagueId,
      type: entity.type,
      amount: entity.amount,
      enabled: entity.enabled,
      createdAt: entity.createdAt,
      updatedAt: entity.updatedAt,
      ...(entity.seasonId !== null && entity.seasonId !== undefined ? { seasonId: entity.seasonId } : {}),
    });
  }
}
