import 'package:drift/drift.dart';
import 'package:niogu_app/core/database/app_database.dart' hide Category;
import 'package:niogu_app/features/goods/products/domain/entities/category.dart';
import 'package:niogu_app/features/goods/products/domain/repositories/i_category_repository.dart';

class CategoryRepositoryImpl implements ICategoryRepository {
  final AppDatabase _appDatabase;

  CategoryRepositoryImpl(this._appDatabase);

  @override
  Stream<List<Category>> watchCategories() {
    return _appDatabase.categoryDao.watchAllCategories();
  }

  @override
  Future<List<Category>> fetchCategories() async {
    return await _appDatabase.categoryDao.getAllCategories();
  }

  @override
  Future<Category?> fetchCategoryById(String id) async {
    return await _appDatabase.categoryDao.getCategoryById(id);
  }

  @override
  Future<void> addCategory(Category category) async {
    final categoryCompanion = CategoriesCompanion(
      localId: Value(category.id),
      name: Value(category.name),
      localImagePath: Value(category.imagePath),
    );

    await _appDatabase.categoryDao.insertCategory(categoryCompanion);
  }

  @override
  Future<void> updateCategory(String id, String name, String? imagePath) async {
    await _appDatabase.categoryDao.updateCategory(id, name, imagePath);
  }
}
