import { RepositoryBase, IRepositoryBase } from '@tomei/general';
import { ProductBrandModel } from '../../entities/product-brand.entity';

export class ProductBrandRepository
  extends RepositoryBase<ProductBrandModel>
  implements IRepositoryBase<ProductBrandModel>
{
  constructor() {
    super(ProductBrandModel);
  }

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

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

  async findAndCountAll(options?: any): Promise<{
    rows: ProductBrandModel[];
    count: number;
  }> {
    try {
      let tags: any;
      if (options) {
        tags = await ProductBrandModel.findAndCountAll(options);
      } else {
        tags = await ProductBrandModel.findAndCountAll();
      }
      return tags;
    } catch (error) {
      throw new Error(`An Error occured when retriving : ${error.message}`);
    }
  }
}
