import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/api/emergency/emergency_notice.dart';

void main() {
  group('EmergencyNotice.parse', () {
    test('parses an active, dismissible notice', () {
      final notice = EmergencyNotice.parse('''
---
active: true
dismissible: true
title: Störung
---
# Ausfall
Der Server ist **down**.
''');

      expect(notice, isNotNull);
      expect(notice!.dismissible, isTrue);
      expect(notice.title, 'Störung');
      expect(notice.body, '# Ausfall\nDer Server ist **down**.');
    });

    test('parses a full-screen (dismissible: false) notice', () {
      final notice = EmergencyNotice.parse('''
---
active: true
dismissible: false
---
Wichtige Meldung
''');

      expect(notice, isNotNull);
      expect(notice!.dismissible, isFalse);
      expect(notice.title, isNull);
    });

    test('defaults dismissible to true when absent', () {
      final notice = EmergencyNotice.parse('''
---
active: true
---
Body
''');

      expect(notice!.dismissible, isTrue);
    });

    test('returns null when active is false', () {
      expect(
        EmergencyNotice.parse('---\nactive: false\n---\nBody'),
        isNull,
      );
    });

    test('returns null when active is missing', () {
      expect(
        EmergencyNotice.parse('---\ntitle: Hallo\n---\nBody'),
        isNull,
      );
    });

    test('ignores comment lines in the frontmatter', () {
      final notice = EmergencyNotice.parse('''
---
# dismissible: false   <- example config, disabled
active: true
dismissible: true
---
Body
''');

      expect(notice, isNotNull);
      expect(notice!.dismissible, isTrue);
    });

    test('returns null without a frontmatter', () {
      expect(EmergencyNotice.parse('Just some **markdown** text'), isNull);
    });

    test('returns null when the closing delimiter is missing', () {
      expect(EmergencyNotice.parse('---\nactive: true\nBody'), isNull);
    });

    test('returns null on empty input', () {
      expect(EmergencyNotice.parse(''), isNull);
      expect(EmergencyNotice.parse('   \n  '), isNull);
    });

    test('returns null when the body is empty', () {
      expect(EmergencyNotice.parse('---\nactive: true\n---\n   '), isNull);
    });

    test('preserves the markdown body verbatim', () {
      final notice = EmergencyNotice.parse('''
---
active: true
---
# Titel

- Punkt 1
- Punkt 2

Mehr [Info](https://example.org).
''');

      expect(
        notice!.body,
        '# Titel\n\n- Punkt 1\n- Punkt 2\n\nMehr [Info](https://example.org).',
      );
    });

    test('handles CRLF line endings', () {
      final notice = EmergencyNotice.parse(
        '---\r\nactive: true\r\n---\r\nHallo\r\n',
      );

      expect(notice, isNotNull);
      expect(notice!.body, 'Hallo');
    });

    test('accepts alternative truthy values for active', () {
      expect(EmergencyNotice.parse('---\nactive: yes\n---\nB'), isNotNull);
      expect(EmergencyNotice.parse('---\nactive: 1\n---\nB'), isNotNull);
      expect(EmergencyNotice.parse('---\nactive: on\n---\nB'), isNotNull);
    });
  });
}
