import 'package:habo/model/category.dart';
import 'package:habo/model/habo_model.dart';
import 'package:habo/repositories/category_repository.dart';

class SqliteCategoryRepository implements CategoryRepository {
  final HaboModel _model;

  SqliteCategoryRepository(this._model);

  @override
  Future<List<Category>> getAllCategories() async {
    final maps = await _model.getAllCategories();
    return maps.map((map) => Category(
      id: map['id'] as int?,
      title: map['title'] as String? ?? '',
      iconCodePoint: map['iconCodePoint'] as int? ?? 0,
      fontFamily: map['fontFamily'] as String?,
    )).toList();
  }

  @override
  Future<int> createCategory(Category category) async {
    return _model.insertCategory(category);
  }

  @override
  Future<void> updateCategory(Category category) async {
    await _model.updateCategory(category);
  }

  @override
  Future<void> deleteCategory(int id) async {
    await _model.deleteCategory(id);
  }

  @override
  Future<void> updateHabitCategories(int habitId, List<Category> categories) async {
    await _model.updateHabitCategories(habitId, categories);
  }

  @override
  Future<List<Category>> getCategoriesForHabit(int habitId) async {
    final maps = await _model.getCategoriesForHabit(habitId);
    return maps.map((map) => Category(
      id: map['id'] as int?,
      title: map['title'] as String? ?? '',
      iconCodePoint: map['iconCodePoint'] as int? ?? 0,
      fontFamily: map['fontFamily'] as String?,
    )).toList();
  }

  @override
  Future<void> deleteAllCategories() async {
    await _model.deleteAllCategories();
  }
}
