import 'package:ambito/src/entity/entities.dart';
import 'package:ambito/src/packages/ambito_db/base_db.dart';
import 'package:isar/isar.dart';

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

class FundingProgramRepository extends BaseDB {
  @override
  IsarCollection collection = isar.fundingPrograms;

  List<FundingProgram> getFundingProgramsByMeasureTypes(List<String> types) {
    final all = isar.fundingPrograms.where().findAll();
    return all.where((FundingProgram fp) {
      return fp.measureType?.any((mt) => types.contains(mt.value)) ?? false;
    }).toList(); // Convert to List<FundingProgram>
  }

  FieldDropdown getDropDownByGroupAndType(String group, String type) {
    final filterItems = isar.fundingPrograms.where().findAll();

    return FieldDropdown(
      name: 'funding_program',
      label: 'Förderprogramm',
      filterValue: ambitoFilterNotifier.activeFilters['funding_program'],
      onClear: () {
        ambitoFilterNotifier.removeFilter('funding_program');
      },
      onSelected: (value) {
        ambitoFilterNotifier.setFilter(
          'funding_program',
          value ?? '',
        );
      },
      entries: filterItems.map((item) => item.name!).toList(),
    );
  }
}
