import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/api/marianumconnect/queries/timetable_get_subjects/timetable_get_subjects_response.dart';
import 'package:marianum_mobile/view/pages/timetable/subject_colors/subject_color_filter.dart';

McSubject _sub(String short, String long, {String? color}) =>
    McSubject(id: short.hashCode, shortName: short, longName: long, color: color);

void main() {
  group('filterAndSortSubjects', () {
    test('keeps only subjects in the plan or already coloured', () {
      final all = [
        _sub('M', 'Mathematik'), // im Plan
        _sub('D', 'Deutsch'), // nicht im Plan, keine Farbe -> raus
        _sub('SP', 'Sport', color: 'teal'), // nicht im Plan, aber gefärbt
      ];
      final result = filterAndSortSubjects(all, {'M'});
      expect(result.map((s) => s.shortName), containsAll(['M', 'SP']));
      expect(result.map((s) => s.shortName), isNot(contains('D')));
    });

    test('sorts coloured subjects first, then alphabetically by label', () {
      final all = [
        _sub('E', 'Englisch'),
        _sub('M', 'Mathematik', color: 'amber'),
        _sub('BIO', 'Biologie'),
        _sub('D', 'Deutsch', color: 'indigo'),
      ];
      final result = filterAndSortSubjects(all, {'E', 'M', 'BIO', 'D'});
      // Gefärbte zuerst (Deutsch, Mathematik alphabetisch), dann der Rest.
      expect(
        result.map((s) => s.shortName).toList(),
        ['D', 'M', 'BIO', 'E'],
      );
    });

    test('drops subjects without a short name', () {
      final all = [_sub('', 'Leer'), _sub('M', 'Mathematik')];
      final result = filterAndSortSubjects(all, {'M', ''});
      expect(result.map((s) => s.shortName), ['M']);
    });
  });

  group('searchSubjects', () {
    final subjects = [
      _sub('M', 'Mathematik'),
      _sub('D', 'Deutsch'),
      _sub('SP', 'Sport'),
    ];

    test('empty query returns everything unchanged', () {
      expect(searchSubjects(subjects, ''), subjects);
      expect(searchSubjects(subjects, '   '), subjects);
    });

    test('matches short name or long name, case-insensitively', () {
      expect(searchSubjects(subjects, 'mathe').single.shortName, 'M');
      expect(searchSubjects(subjects, 'sp').single.shortName, 'SP');
      expect(searchSubjects(subjects, 'DEUTSCH').single.shortName, 'D');
    });

    test('no match returns empty', () {
      expect(searchSubjects(subjects, 'chemie'), isEmpty);
    });
  });
}
