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

const schemaOrderPdfPrintJob = joi.object({
  shopId: joiString.required(),
  printId: joi.string().required(),
  shopName: joi.string().optional().allow('').default(''),
  page: joi.number().integer().min(0).default(0),
  size: joi.number().integer().min(0).default(0),
  type: joi.string().optional().default('pdf'),
  platform: joi.string().valid('shopee', 'tiktok', 'lazada', 'tiki').required(),
  search: joi.string().optional().allow('').default(''),
  text: joi.string().optional().allow('').default(''),
  s3Id: joi.string().optional().allow('').default(''),
  s3Url: joi.string().optional().allow('').default(''),
  s3Path: joi.string().optional().allow('').default(''),
  createdAt: joi.number().default(() => Date.now()),
  updatedAt: joi.number().default(() => Date.now()),
});

export const ShopeeOrderPdfPrintJob = new ModelApiLoader('shopee_order_print_log', schemaOrderPdfPrintJob);
export const LazadaOrderPdfPrintJob = new ModelApiLoader('lazada_order_print_log', schemaOrderPdfPrintJob);
export const TiktokOrderPdfPrintJob = new ModelApiLoader('tiktok_order_print_log', schemaOrderPdfPrintJob);

export const loaderIndex = () => {
  return Promise.allSettled([
    ShopeeOrderPdfPrintJob.createIndex({
      index: {
        fields: ['shopId', 'search', 'createdAt'],
      },
    }),
    LazadaOrderPdfPrintJob.createIndex({
      index: {
        fields: ['shopId', 'search', 'createdAt'],
      },
    }),
    TiktokOrderPdfPrintJob.createIndex({
      index: {
        fields: ['shopId', 'search', 'createdAt'],
      },
    }),
  ]);
};
