import { describe, expect, it } from 'vitest';

import { Money } from '@core/racing/domain/value-objects/Money';

import { TypeOrmPersistenceSchemaError } from '../errors/TypeOrmPersistenceSchemaError';
import { MoneyOrmMapper } from './MoneyOrmMapper';

describe('MoneyOrmMapper', () => {
  it('toOrm maps Money to plain object', () => {
    const mapper = new MoneyOrmMapper();
    const money = Money.create(12.5, 'EUR');

    expect(mapper.toOrm(money)).toEqual({ amount: 12.5, currency: 'EUR' });
  });

  it('toDomain validates schema and throws adapter-scoped base schema error type', () => {
    const mapper = new MoneyOrmMapper();

    try {
      mapper.toDomain('LeagueWallet', 'balance', { amount: 10, currency: 'JPY' });
      throw new Error('expected-to-throw');
    } catch (error) {
      expect(error).toBeInstanceOf(TypeOrmPersistenceSchemaError);
      expect(error).toMatchObject({
        entityName: 'LeagueWallet',
        fieldName: 'balance.currency',
        reason: 'invalid_enum_value',
      });
    }
  });

  it('toDomain wraps domain validation failures into adapter-scoped schema error type', () => {
    const mapper = new MoneyOrmMapper();

    try {
      mapper.toDomain('LeagueWallet', 'balance', { amount: -1, currency: 'USD' });
      throw new Error('expected-to-throw');
    } catch (error) {
      expect(error).toBeInstanceOf(TypeOrmPersistenceSchemaError);
      expect(error).toMatchObject({
        entityName: 'LeagueWallet',
        fieldName: 'balance',
        reason: 'invalid_shape',
      });
    }
  });
});