import 'package:ambito/src/entity/base_entity.dart';
import 'package:ambito/src/entity/measure/measure_repository.dart';
import 'package:isar/isar.dart';
import 'package:json_annotation/json_annotation.dart';

import '../../../packages/ambito_notifier/notifier/filter_notifier.dart';
import '../../../widgets/form/fields/field_dropdown.dart';

part 'measure_group.g.dart';

@collection
@JsonSerializable(explicitToJson: true)
class MeasureGroup extends BaseEntity with EntityWithIdAndName {
  MeasureGroup();

  @Index()
  String? url;
  String? image;
  String? description;
  List<String> categories = [];
  List<int> ids = [];
  int children = 0;

  factory MeasureGroup.fromJson(Map<String, dynamic> json) =>
      _$MeasureGroupFromJson(json);

  Map<String, dynamic> toJson() => _$MeasureGroupToJson(this);
}

extension MeasureGroupExtension on MeasureGroup {
  FieldDropdown getDropDownByCategory(String category) {
    List<MeasureGroup> filterItems =
        MeasureRepository().getGroupsByCategory(category);

    return FieldDropdown(
      name: 'group',
      label: 'Maßnahmengruppe',
      filterValue: ambitoFilterNotifier.activeFilters['group'],
      onClear: () => ambitoFilterNotifier.removeFilter('group'),
      onSelected: (value) =>
          ambitoFilterNotifier.setFilter('group', value ?? ''),
      entries: filterItems.map((item) => item.name!).toList(),
    );
  }
}
