import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/api/marianumcloud/talk/chat/get_chat_response.dart';
import 'package:marianum_mobile/api/marianumcloud/talk/chat/rich_object_string_processor.dart';

RichObjectString _r(
  String name, {
  RichObjectStringObjectType type = RichObjectStringObjectType.user,
}) => RichObjectString(type, 'id-$name', name, null, null);

void main() {
  group('RichObjectStringProcessor.parseToString', () {
    test('null data returns the message unchanged', () {
      expect(
        RichObjectStringProcessor.parseToString('Hallo {actor}', null),
        'Hallo {actor}',
      );
    });

    test('substitutes a single placeholder by .name', () {
      expect(
        RichObjectStringProcessor.parseToString(
          '{actor} hat eine Datei geteilt',
          {'actor': _r('Elias')},
        ),
        'Elias hat eine Datei geteilt',
      );
    });

    test('substitutes multiple placeholders independently', () {
      expect(
        RichObjectStringProcessor.parseToString(
          '{actor} hat {file} mit {target} geteilt',
          {
            'actor': _r('Elias'),
            'file': _r('foo.pdf', type: RichObjectStringObjectType.file),
            'target': _r('Klasse 11a', type: RichObjectStringObjectType.group),
          },
        ),
        'Elias hat foo.pdf mit Klasse 11a geteilt',
      );
    });

    test('replaces every occurrence of the same placeholder', () {
      expect(
        RichObjectStringProcessor.parseToString('{actor} {actor} {actor}', {
          'actor': _r('A'),
        }),
        'A A A',
      );
    });

    test('placeholders with no matching key remain unchanged', () {
      expect(
        RichObjectStringProcessor.parseToString('{actor} sah {file}', {
          'actor': _r('Elias'),
        }),
        'Elias sah {file}',
      );
    });

    test('empty data map returns the message unchanged', () {
      expect(
        RichObjectStringProcessor.parseToString('Hallo {actor}', const {}),
        'Hallo {actor}',
      );
    });

    test('messages without placeholders are returned verbatim', () {
      expect(
        RichObjectStringProcessor.parseToString('reine Textnachricht', {
          'actor': _r('A'),
        }),
        'reine Textnachricht',
      );
    });
  });
}
