import 'dart:async';

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:niogu_app/features/goods/units/domain/entities/unit.dart';
import 'package:niogu_app/features/goods/units/domain/repositories/i_unit_repository.dart';
import 'package:niogu_app/features/goods/units/presentation/providers/unit_provider.dart';

class UnitController extends AutoDisposeAsyncNotifier<void> {
  late IUnitRepository _unitRepository;

  @override
  FutureOr<void> build() {
    _unitRepository = ref.read(unitRepositoryProvider);
  }

  Future<void> addUnit(Unit unit) async {
    state = const AsyncValue.loading();

    state = await AsyncValue.guard(() async {
      await _unitRepository.addUnit(unit);
    });
  }

  Future<void> updateUnit(String id, String name) async {
    state = const AsyncValue.loading();

    state = await AsyncValue.guard(() async {
      await _unitRepository.updateUnit(id, name);
    });
  }

}
