import 'package:test/test.dart';

import 'package:pshared/data/dto/describable.dart';
import 'package:pshared/data/dto/ledger/account.dart';
import 'package:pshared/data/dto/ledger/role.dart';
import 'package:pshared/data/dto/ledger/status.dart';
import 'package:pshared/data/dto/ledger/type.dart';
import 'package:pshared/data/mapper/ledger/account.dart';

LedgerAccountDTO _baseDto({
  DescribableDTO? describable,
  Map<String, String>? metadata,
}) {
  return LedgerAccountDTO(
    ledgerAccountRef: 'ledger:r1',
    organizationRef: 'org:1',
    accountCode: 'asset:rub:abc',
    accountType: LedgerAccountTypeDTO.asset,
    currency: 'RUB',
    status: LedgerAccountStatusDTO.active,
    allowNegative: false,
    role: LedgerAccountRoleDTO.operating,
    metadata: metadata,
    describable: describable,
  );
}

void main() {
  group('LedgerAccountDTOMapper', () {
    test('keeps describable name when it is present', () {
      final dto = _baseDto(
        describable: const DescribableDTO(name: 'Main RUB', description: 'ops'),
        metadata: const {'name': 'Metadata Name'},
      );

      final domain = dto.toDomain();

      expect(domain.name, equals('Main RUB'));
      expect(domain.description, equals('ops'));
    });

    test('uses metadata name when describable is absent', () {
      final dto = _baseDto(metadata: const {'name': 'Metadata Name'});

      final domain = dto.toDomain();

      expect(domain.name, equals('Metadata Name'));
      expect(domain.description, isNull);
    });

    test('uses metadata name when describable name is blank', () {
      final dto = _baseDto(
        describable: const DescribableDTO(name: '   ', description: 'ops'),
        metadata: const {'name': 'Metadata Name'},
      );

      final domain = dto.toDomain();

      expect(domain.name, equals('Metadata Name'));
      expect(domain.description, equals('ops'));
    });
  });
}
