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

export const getDefaultMarketingSenderConfig = () => ({
  delayBetweenSeconds: 5,
  pauseAfterCount: 10,
  pauseDurationSeconds: 60,
  duplicateCooldownMinutes: 1440,
  maxSendCount: 100,
});

export const getDefaultMarketingSenderContent = () => [
  { text: '', payload: { stickerId: '0029', stickerPackage: 'shrimp_vn_new' }, type: 'sticker' },
  { text: 'Xin chào quý khách hàng', payload: {}, type: 'text' },
];

const schemaConfigSend = joi.object({
  delayBetweenSeconds: joi.number().integer().min(0).default(5),
  pauseAfterCount: joi.number().integer().min(1).default(10),
  pauseDurationSeconds: joi.number().integer().min(0).default(60),
  duplicateCooldownMinutes: joi.number().integer().min(0).default(60),
  maxSendCount: joi.number().integer().min(0).default(0),
}).default(getDefaultMarketingSenderConfig);

const schemaListContent = joi.array().items(joi.object({
  text: joi.string().optional().default('').allow(''),
  payload: joi.object().optional().default({}),
  type: joi.string().valid('text', 'image', 'sticker', 'product').required(),
})).optional().default([]);

const schemaMarketingSenderCollect = joi.object({
  name: joi.string().required(),
  shopId: joiString.required(),
  shopName: joi.string().optional().default(''),
  status: joi.string().valid('pending', 'sending', 'completed', 'failed').default('pending'),
  totalCustomers: joi.number().integer().default(0),
  sentCount: joi.number().integer().default(0),
  failedCount: joi.number().integer().default(0),
  createdAt: joi.number().default(() => Date.now()),
  updatedAt: joi.number().default(() => Date.now()),
  configSend: schemaConfigSend.optional().default(getDefaultMarketingSenderConfig),
  listContent: schemaListContent.optional().default(getDefaultMarketingSenderContent()),
});

const schemaMarketingSenderCustomer = joi.object({
  collectId: joiString.required(),
  toUserId: joi.number().integer().min(0).allow(0)
    .required(),
  toShopId: joi.number().integer().min(0).allow(0)
    .required(),
  customerName: joi.string().optional().default('').allow(''),
  source: joi.string().optional().default('').allow(''),
  active: joi.boolean().default(true),
  message: joi.string().optional().default('').allow(''),
  status: joi.string().valid('pending', 'sent', 'skipped', 'failed').default('pending'),
  error: joi.string().optional().default('').allow(''),
  sentAt: joi.number().optional().allow(null),
  username: joi.string().optional().default('').allow(''),
  createdAt: joi.number().default(() => Date.now()),
  updatedAt: joi.number().default(() => Date.now()),
});

export const ShopeeMarketingSenderCollect = new ModelApiLoader('shopee_marketing_sender_collect', schemaMarketingSenderCollect);
export const ShopeeMarketingSenderCustomer = new ModelApiLoader('shopee_marketing_sender_customer', schemaMarketingSenderCustomer);

export const loaderIndex = () => {
  return Promise.allSettled([
    ShopeeMarketingSenderCollect.createIndex({
      index: {
        fields: ['shopId', 'status', 'createdAt'],
      },
    }),
    ShopeeMarketingSenderCustomer.createIndex({
      index: {
        fields: ['collectId', 'toUserId'],
      },
    }),
  ]);
};
