import 'package:diameter/main.dart';
import 'package:diameter/models/accuracy.dart';
import 'package:diameter/models/meal_category.dart';
import 'package:diameter/models/meal_portion_type.dart';
import 'package:objectbox/objectbox.dart';
import 'package:diameter/objectbox.g.dart' show MealSource_;

@Entity(uid: 1283034494527412242)
@Sync()
class MealSource {
  static final Box<MealSource> box = objectBox.store.box<MealSource>();

  // properties
  int id;
  bool deleted;
  String value;
  String? notes;

  // relations
  final defaultMealCategory = ToOne<MealCategory>();
  final defaultMealPortionType = ToOne<MealPortionType>();
  final defaultCarbsRatioAccuracy = ToOne<Accuracy>();
  final defaultPortionSizeAccuracy = ToOne<Accuracy>();

  // constructor
  MealSource({
    this.id = 0,
    this.deleted = false,
    this.value = '',
    this.notes,
  });

  // methods
  static MealSource? get(int id) => box.get(id);
  static void put(MealSource mealSource) => box.put(mealSource);
  
  static List<MealSource> getAll() {
    QueryBuilder<MealSource> builder = box.query(MealSource_.deleted.equals(false))..order(MealSource_.value);
    return builder.build().find();
  }
  
  static void remove(int id) {
    final item = box.get(id);
    if (item != null) {
      item.deleted = true;
      box.put(item);
    }
  }

  @override
  String toString() {
    return value;
  }
}
