import 'package:drift/drift.dart';
import 'package:niogu_app/core/database/app_database.dart';
import 'package:niogu_app/features/supplier/domain/entities/supplier.dart';
import 'package:niogu_app/features/supplier/domain/repositories/i_supplier_repository.dart';

class SupplierRepositoryImpl implements ISupplierRepository {
  final AppDatabase _appDatabase;

  SupplierRepositoryImpl(this._appDatabase);

  @override
  Stream<List<DisplaySuppliers>> watchSuppliers() {
    return _appDatabase.supplierDao.watchAllSuppliers();
  }

  @override
  Future<SupplierDetail> fetchSupplierById(String id) async {
    return await _appDatabase.supplierDao.getSupplierById(id);
  }

  @override
  Future<int> saveSupplier(UpsertSupplier supplier) async {
    return await _appDatabase.supplierDao.saveSupplier(
      SuppliersCompanion(
        localId: Value(supplier.id),
        name: Value(supplier.name),
        email: Value(supplier.email),
        phoneNumber: Value(supplier.phoneNumber),
      ),
    );
  }

  @override
  Future<int> deleteSupplier(String id) async {
    return await _appDatabase.supplierDao.deleteSupplier(id);
  }
}
