import lodash from 'lodash';
import i18n from '@/vue/i18n';
import { addCommas, calcDiscount } from '@/lib/utils/str';
import { getDateOnly, getYearOnly } from '@/lib/utils/moment';
import {
  productUrl, productSeller, shopUrlId, imageUrl,
} from '@/adapter/shopee/render/utils';

export const tableProduct = {
  columns: [
    {
      type: 'selection',
      width: 60,
      align: 'center',
    },
    {
      title: '#',
      key: 'index',
      sortable: true,
      width: 70,
    },
    {
      title: i18n.t('PRODUCT_SKU'),
      key: 'sku',
      sortable: true,
      width: 100,
    },
    {
      title: i18n.t('PRODUCT_NAME'),
      key: 'name',
      slot: 'name',
      sortable: true,
      width: 400,
    },
    {
      title: i18n.t('PRODUCT_IMAGE'),
      key: 'image',
      slot: 'image',
      width: 80,
    },
    {
      title: i18n.t('PRODUCT_BRAND'),
      key: 'brand',
      sortable: true,
    },
    {
      title: i18n.t('PRODUCT_CATEGORY'),
      key: 'categoryName',
      sortable: true,
    },
    {
      title: i18n.t('PRODUCT_TIME_CREATE'),
      key: 'showTime',
      sortable: true,
    },
    {
      title: i18n.t('PRODUCT_MODEL_PRICE'),
      key: 'price',
      render: (h, params) => {
        return h('span', addCommas(params.row.price));
      },
    },
    {
      title: i18n.t('PRODUCT_MODEL_DISCOUNT'),
      key: 'discount',
      sortable: true,
      render: (h, params) => {
        return h('span', `${params.row.discount}%`);
      },
    },
    {
      title: i18n.t('PRODUCT_MODEL_STOCK'),
      key: 'stock',
      sortable: true,
      render: (h, params) => {
        return h('span', addCommas(params.row.stock));
      },
    },
    {
      title: i18n.t('PRODUCT_DIMENSION'),
      key: 'dimensionWidth',
      render: (h, params) => {
        return h('span', [params.row.dimensionLength, params.row.dimensionWidth, params.row.dimensionHeight].join('/'));
      },
      sortable: true,
    },
    {
      title: i18n.t('PRODUCT_WEIGHT'),
      key: 'dimensionWeight',
      sortable: true,
    },
    {
      title: i18n.t('PRODUCT_MODEL_SOLD'),
      key: 'sold',
      sortable: true,
      render: (h, params) => {
        return h('span', addCommas(params.row.sold));
      },
    },
    {
      title: i18n.t('TABLE_ACTION'),
      slot: 'action',
    },
  ],
  rows: [],
};

/**
 *
 * @param {ShopeeSellerProductId} product
 * @param {number} index
 */
export const renderTableProduct = (product, index = 0) => {
  const productId = product.id;
  const shopId = product.shop_id;

  const firstModel = product.model_list.find((model) => model.is_default) || product.model_list[0];

  const sku = product.parent_sku || firstModel.sku || productId;

  const price = Number(firstModel.price_info.input_normal_price);
  const promotion = Number(firstModel.price_info.input_promotion_price) || price;
  const discount = calcDiscount(price, promotion);

  const stock = lodash.sumBy(product.model_list, (item) => (item.stock_detail ? item.stock_detail.total_available_stock : 0));

  const dimensionWeight = product.weight.value || 0;
  const dimensionWidth = product.dimension.width || 0;
  const dimensionHeight = product.dimension.height || 0;
  const dimensionLength = product.dimension.length || 0;

  const sold = product.statistics.sold_count;

  const categoryId = product.category_path.at(-1);
  const categoryData = {
    id: categoryId,
    name: product.category_path_name_list.at(-1),
    tree: product.category_path_name_list.map((name) => name).join(' > '),
  };

  const brand = product.brand_info.brand_name;
  const timeCreate = new Date(product.create_time * 1000);

  return {
    unlisted: product.tag.unlist,
    index: index + 1,
    productId,
    shopId,
    sku,
    skuList: product.model_list.map((item) => item.sku),
    categoryId: categoryData.id,
    brand,
    categoryName: categoryData.name,
    categoryTree: categoryData.tree,
    productUrl: productUrl(productId, shopId),
    sellerUrl: productSeller(productId, shopId),
    shopUrl: shopUrlId(shopId),
    name: product.name,
    image: imageUrl(product.cover_image),
    timeCreate,
    showTime: getDateOnly(timeCreate),
    year: getYearOnly(timeCreate),
    price,
    stock,
    dimensionWeight,
    dimensionWidth,
    dimensionHeight,
    dimensionLength,
    promotion,
    sale: sold * price,
    sold,
    discount,
    modelCount: product.model_list.length,
    likeCount: product.statistics.liked_count || 0,
    viewCount: product.statistics.view_count || 0,
    commentCount: 0,
  };
};
