import { ModelApiLoader } from '@/api/model';
import joi, { joiString } from '@/validator/joi';

const schemaUsageProduct = joi.object({
  type: joi.string().required(),
  shopId: joiString.required(),
  payload: joi.object().default({}).optional(),
  result: joi.object().default({}).optional(),
});

export const UsageShopee = new ModelApiLoader('usage_shopee', schemaUsageProduct);
export const UsageTiktok = new ModelApiLoader('usage_tiktok', schemaUsageProduct);

export const loaderIndex = () => {
  return Promise.allSettled([
    UsageShopee.createIndex({
      index: {
        fields: ['createdAt'],
      },
    }),
    UsageShopee.createIndex({
      index: {
        fields: ['type', 'shopId'],
      },
    }),
    UsageTiktok.createIndex({
      index: {
        fields: ['type', 'shopId'],
      },
    }),
    UsageTiktok.createIndex({
      index: {
        fields: ['createdAt'],
      },
    }),
  ]);
};
