import 'dart:async';

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:niogu_app/features/stock_check/domain/repositories/i_stock_checked_repository.dart';
import 'package:niogu_app/features/stock_check/presentation/providers/stock_check_provider.dart';
import 'package:niogu_app/features/stock_in/domain/entities/stock_in.dart';

class StockCheckController extends AutoDisposeAsyncNotifier<void> {
  late final IStockCheckedRepository _stockCheckedRepository;

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

  Future<void> updateStockItem(List<UpdateStockItem> items) async {
    state = const AsyncValue.loading();

    final result = await AsyncValue.guard(() async {
      await _stockCheckedRepository.updateStockItem(items);
    });

    state = result;

    if (result is AsyncError) throw result.error!;
  }
}
