import 'package:drift/drift.dart';
import 'package:niogu_app/core/database/app_database.dart' hide Unit;
import 'package:niogu_app/features/goods/units/domain/entities/unit.dart';
import 'package:niogu_app/features/goods/units/domain/repositories/i_unit_repository.dart';

class UnitRepositoryImpl implements IUnitRepository {
  final AppDatabase _appDatabase;

  UnitRepositoryImpl(this._appDatabase);

  @override
  Stream<List<Unit>> watchUnits() {
    return _appDatabase.unitDao.watchAllUnits();
  }

  @override
  Future<List<Unit>> fetchUnits() async {
    return await _appDatabase.unitDao.getAllUnits();
  }

  @override
  Future<Unit?> fetchUnitById(String id) async {
    return await _appDatabase.unitDao.getUnitById(id);
  }

  @override
  Future<void> addUnit(Unit unit) async {
    final unitCompanion = UnitsCompanion(
      localId: Value(unit.id),
      name: Value(unit.name),
    );

    await _appDatabase.unitDao.insertUnit(unitCompanion);
  }

  @override
  Future<void> updateUnit(String id, String name) async {
    await _appDatabase.unitDao.updateUnit(id, name);
  }
}
