import moment from 'moment';
import { ConfigPdfPrinter } from '@/models/config';
import { ShopeeSpyList, ShopeeSpyLog } from './shopee/spy';
import { ShopeeFlashSaleSave } from './shopee/seller/flashsale';
import { UsageShopee, UsageTiktok } from './usage';
import { GroupProductList } from './group';
import {
  ShopeeOrderPdfPrintJob,
  LazadaOrderPdfPrintJob,
  TiktokOrderPdfPrintJob,
} from './orderPdf';

import { loaderIndex as loaderIndexTiktokShop } from './tiktok/shop';
import { loaderIndex as loaderIndexTiktokProduct } from './tiktok/product';
import { loaderIndex as loaderIndexShopeeShop } from './shopee/shop';
import { loaderIndex as loaderIndexShopeeProduct } from './shopee/product';
import { loaderIndex as loaderIndexShopeeSellerShop } from './shopee/seller/shop';
import { loaderIndex as loaderIndexShopeeSellerProduct } from './shopee/seller/product';
import { loaderIndex as loaderIndexShopeeSellerFlashsale } from './shopee/seller/flashsale';
import { loaderIndex as loaderIndexShopeeMarketingSender } from './shopee/marketing/sender';
import { loaderIndex as loaderIndexGroup } from './group';
import { loaderIndex as loaderIndexMapping } from './mapping';
import { loaderIndex as loaderIndexUsage } from './usage';
import { loaderIndex as loaderIndexOrderPdf } from './orderPdf';

const indexLoaders = [
  loaderIndexTiktokShop,
  loaderIndexTiktokProduct,
  loaderIndexShopeeShop,
  loaderIndexShopeeProduct,
  loaderIndexShopeeSellerShop,
  loaderIndexShopeeSellerProduct,
  loaderIndexShopeeSellerFlashsale,
  loaderIndexShopeeMarketingSender,
  loaderIndexGroup,
  loaderIndexMapping,
  loaderIndexUsage,
  loaderIndexOrderPdf,
];

export const pullAllDatabaseRemote = () => {
  return Promise.allSettled([
    ConfigPdfPrinter.pullRemote(),
    ShopeeSpyList.pullRemote(),
    ShopeeSpyLog.pullRemote(),
    ShopeeFlashSaleSave.pullRemote(),
    UsageShopee.pullRemote(),
    UsageTiktok.pullRemote(),
    GroupProductList.pullRemote(),
    ShopeeOrderPdfPrintJob.pullRemote(),
    LazadaOrderPdfPrintJob.pullRemote(),
    TiktokOrderPdfPrintJob.pullRemote(),
  ]);
};

export const modelCleanerData = () => {
  const cleanQuery30DaysAgo = {
    createdAt: {
      $lt: moment().subtract(30, 'days').toISOString(),
    },
  };
  const cleanQuery45DaysAgo = {
    createdAt: {
      $lt: moment().subtract(45, 'days').toISOString(),
    },
  };

  return Promise.allSettled([
    UsageShopee.removeQuery(cleanQuery30DaysAgo),
    UsageTiktok.removeQuery(cleanQuery30DaysAgo),
    ShopeeOrderPdfPrintJob.removeQuery(cleanQuery45DaysAgo),
    LazadaOrderPdfPrintJob.removeQuery(cleanQuery45DaysAgo),
    TiktokOrderPdfPrintJob.removeQuery(cleanQuery45DaysAgo),
  ]);
};

export const modelIndexLoader = () => {
  return Promise.allSettled(
    indexLoaders.map((loader) => loader()),
  );
};

export const destroyAllDatabase = async () => {
  let dbList = [];

  if (indexedDB.databases) {
    dbList = (await indexedDB.databases()).map((database) => database.name);
  } else if (indexedDB.webkitGetDatabaseNames) {
    dbList = await new Promise((resolve) => {
      const req = indexedDB.webkitGetDatabaseNames();
      req.onsuccess = () => resolve(Array.from(req.result));
    });
  }

  const pouchDbs = dbList.filter((name) => name && name.startsWith('_pouch_'));

  await Promise.all(
    pouchDbs.map((name) => indexedDB.deleteDatabase(name)),
  );

  return pouchDbs;
};
