import { RepositoryBase, IRepositoryBase } from '@tomei/general';
import { ProductVariantWithInventoryModel } from '../../entities/product-variant-with-inventory.entity';

export class ProductVariantWithInventoryRepository
  extends RepositoryBase<ProductVariantWithInventoryModel>
  implements IRepositoryBase<ProductVariantWithInventoryModel>
{
  constructor() {
    super(ProductVariantWithInventoryModel);
  }

  async findByPk(
    variantId: string,
    transaction: any,
  ): Promise<ProductVariantWithInventoryModel> {
    try {
      const options = {
        where: {
          VariantId: variantId,
        },
        transaction,
      };
      const result = await ProductVariantWithInventoryModel.findOne(options);
      return result;
    } catch (error) {
      throw new Error(`An Error occured when fetching : ${error.message}`);
    }
  }

  async delete(variantId: string, transaction?: any) {
    try {
      const options = {
        where: {
          VariantId: variantId,
        },
        transaction,
      };
      await ProductVariantWithInventoryModel.destroy(options);
    } catch (error) {
      throw new Error(`An Error occured when delete : ${error.message}`);
    }
  }

  async findAndCountAll(options?: any) {
    try {
      let vwi: any;
      if (options) {
        vwi = await ProductVariantWithInventoryModel.findAndCountAll(options);
      } else {
        vwi = await ProductVariantWithInventoryModel.findAndCountAll();
      }
      return vwi;
    } catch (error) {
      throw new Error(`An Error occured when retriving : ${error.message}`);
    }
  }
}
