import 'package:drift/drift.dart';
import 'package:niogu_app/core/database/app_database.dart';
import 'package:niogu_app/core/enums/item_type.dart';
import 'package:niogu_app/core/enums/purchase_status.dart';
import 'package:niogu_app/core/enums/sync_status.dart';
import 'package:niogu_app/core/system/system_setting.dart';
import 'package:niogu_app/features/stock_in/domain/entities/stock_in.dart';
import 'package:niogu_app/features/stock_in/domain/repositories/i_stock_in_repository.dart';

class StockInRepositoryImpl implements IStockInRepository {
  final AppDatabase _appDatabase;

  StockInRepositoryImpl(this._appDatabase);

  @override
  Stream<List<DisplayItem>> watchItems(ItemType itemType, String outletId) {
    return _appDatabase.outletInventoryDao.watchAllItems(itemType);
  }

  @override
  Future<void> updateStockItem(
    SupplierInformation? supplierInfo,
    NewPurchase? newPurchase,
    List<ItemPurchase>? itemPurchases,
    List<UpdateStockItem> items,
  ) async {
    SuppliersCompanion? supplier;

    if (supplierInfo != null) {
      supplier = SuppliersCompanion(
        localId: Value(supplierInfo.localId),
        name: Value(supplierInfo.name),
        phoneNumber: Value(supplierInfo.phoneNumber),
        // address: Value(supplierInfo.address),
        syncStatus: Value(SyncStatus.dirty.status),
      );
    }

    PurchasesCompanion? purchase;

    final List<PurchaseItemsCompanion> purchaseItems = [];

    if (newPurchase != null && itemPurchases != null) {
      final outletId = await SystemSetting.getCurrentOutletId();

      final outletName = await SystemSetting.getCurrentOutletName();

      final userId = await SystemSetting.getCurrentUserId();

      final userName = await SystemSetting.getCurrentUserName();

      purchase = PurchasesCompanion(
        localId: Value(newPurchase.localId),
        outletId: Value(outletId!),
        outletNameSnapshot: Value(outletName!),
        supplierId: Value(newPurchase.supplierId),
        supplierNameSnapshot: Value(newPurchase.supplierNameSnapshot),
        supplierPhoneNumberSnapshot: Value(
          newPurchase.supplierPhoneNumberSnapshot,
        ),
        userId: Value(userId!),
        adminNameSnapshot: Value(userName!),
        purchaseStatus: Value(PurchaseStatus.completed.status),
        totalPurchase: Value(newPurchase.totalPurchase),
        discount: Value(newPurchase.otherInformation.discount),
        tax: Value(newPurchase.otherInformation.tax),
        paymentStatus: Value(newPurchase.paymentStatus.status),
        totalAmount: Value(newPurchase.totalAmount),
        amountPaid: Value(newPurchase.amountPaid),
        changeAmount: Value(newPurchase.changeAmount),
        underPaymentAmount: Value(newPurchase.underPaymentAmount),
        debtAmount: Value(newPurchase.debtAmount),
        paymentProofPath: Value(newPurchase.paymentProofPath),
        notes: Value(newPurchase.otherInformation.note),
        syncStatus: Value(newPurchase.syncStatus.status),
      );

      for (final item in itemPurchases) {
        purchaseItems.add(
          PurchaseItemsCompanion(
            localId: Value(item.localId),
            purchaseId: Value(item.purchaseId),
            itemType: Value(item.itemType.type),
            itemId: Value(item.itemId),
            quantity: Value(item.quantity),
            itemImageSnapshot: Value(item.imagePath),
            itemNameSnapshot: Value(item.itemNameSnapshot),
            itemVariantNameSnapshot: Value(item.itemVariantNameSnapshot),
            purchasePriceSnaphot: Value(item.purchasePriceSnaphot),
            subtotal: Value(item.subtotal),
            syncStatus: Value(item.syncStatus.status),
          ),
        );
      }
    }

    return _appDatabase.stockCardDao.updateStockItem(
      supplier: supplier,
      purchase: purchase,
      purchaseItem: purchaseItems,
      items: items,
    );
  }
}
