import {
  DATA_FORMAT,
  DATA_TYPE,
  FILTER_FIELD_TYPE,
  FILTER_TYPE,
  REPORT_GROUP,
} from '../../../constant';
import { ReportConfigEntity } from '../../../entities/report-config.entity';
import { TransactionType } from 'src/modules/transaction/transaction/constants';
import { STATUS } from 'src/core/strings/constants/base.constants';

export default <ReportConfigEntity>{
  group_name: REPORT_GROUP.transaction_report,
  unique_name: `${REPORT_GROUP.transaction_report}__income_per_item_master`,
  label: 'Pendapatan Per Item Master',
  table_schema: `transactions main
    LEFT JOIN transaction_items tr_item ON tr_item.transaction_id = main.id
    LEFT JOIN transaction_item_breakdowns tr_item_bundling ON tr_item_bundling.transaction_item_id = tr_item.id
    LEFT JOIN refunds refund ON refund.transaction_id = main.id
    LEFT JOIN refund_items refund_item ON refund_item.refund_item_id = tr_item.item_id::uuid
    LEFT JOIN items item ON item.id::text = tr_item.item_id::text
    LEFT JOIN time_groups tg on tg.id = item.time_group_id
    LEFT JOIN users tenant ON tenant.id::text = item.tenant_id::text`,
  main_table_alias: 'main',
  whereDefaultConditions: [
    {
      column: 'main.status',
      filter_type: FILTER_TYPE.TEXT_IN_MEMBER,
      values: [STATUS.SETTLED, STATUS.REFUNDED, STATUS.PROCESS_REFUND],
    },
    {
      column: 'main.is_recap_transaction',
      filter_type: FILTER_TYPE.TEXT_EQUAL,
      values: [false],
    },
  ],
  defaultOrderBy: [
    `main.updated_at DESC`,
    `to_char(main.payment_date, 'DD-MM-YYYY') DESC`,
  ],
  lowLevelOrderBy: [
    `main.updated_at DESC`,
    `to_char(main.payment_date, 'DD-MM-YYYY') DESC`,
    `tr_item.id DESC`,
    `tr_item_bundling.id DESC`,
  ],
  filter_period_config: {
    hidden: true,
  },

  column_configs: [
    {
      column: 'main__payment_date',
      query: `to_char(main.payment_date, 'DD-MM-YYYY')`,
      label: 'Tgl. Pendapatan',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__status',
      query: 'main.status',
      label: 'Status',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.STATUS,
    },
    {
      column: 'item_owner',
      query: `CASE WHEN tenant.name is not null THEN tenant.name ELSE 'Company' END`,
      label: 'Kepemilikan',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__type',
      query: 'main.type',
      label: 'Sumber',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__invoice_code',
      query: `CASE WHEN main.type != 'counter' THEN main.invoice_code ELSE null END`,
      label: 'Kode Booking',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__payment_code',
      query: `CASE WHEN main.type = 'counter' THEN main.invoice_code ELSE main.payment_code END`,
      label: 'Kode Pembayaran',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'tr_item__item_category_name',
      query: 'tr_item.item_category_name',
      label: 'Kategori Item',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'tr_item__item_type',
      query: 'tr_item.item_type',
      label: 'Tipe Item',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'tr_item_bundling__item_name',
      query: `CASE WHEN tr_item.item_type = 'bundling' THEN tr_item.item_name ELSE tr_item_bundling.item_name END`,
      label: 'Nama Item Bundling',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'tg__name',
      query: 'tg.name',
      label: 'Time Group',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'tr_item__item_name',
      query: `CASE WHEN tr_item.item_type = 'bundling' THEN tr_item_bundling.item_name ELSE tr_item.item_name END`,
      label: 'Nama Item ',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__customer_type',
      query: 'main.customer_type',
      label: 'Tipe Pelanggan',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__creator_counter_no',
      query: 'main.creator_counter_no',
      label: 'No.PoS',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__creator_counter_name',
      query: 'main.creator_counter_name',
      label: 'Nama PoS',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'tr_item__qty',
      query: 'tr_item.qty',
      label: 'Qty',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.NUMBER,
    },
    {
      column: 'tr_item_bundling__hpp',
      // query: 'tr_item_bundling.hpp',
      query: `CASE WHEN tr_item.item_type != 'bundling' THEN tr_item.total_hpp ELSE tr_item_bundling.hpp END`,
      label: 'Total HPP',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'tr_item_bundling__total_price',
      // query: 'tr_item_bundling.total_price',
      query: `CASE WHEN tr_item.item_type != 'bundling' THEN tr_item.total_price ELSE tr_item_bundling.total_price END`,
      label: 'Subtotal',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'tr_item_bundling__discount_value',
      // query: 'tr_item_bundling.discount_value',
      query: `CASE WHEN tr_item.item_type != 'bundling' THEN tr_item.discount_value ELSE tr_item_bundling.discount_value END`,
      label: 'Diskon (IDR)',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'tr_item_bundling__total_net_price',
      // query: 'tr_item_bundling.total_net_price',
      query: `CASE WHEN tr_item.item_type != 'bundling' THEN tr_item.total_net_price ELSE tr_item_bundling.total_net_price END`,
      label: 'Total Penjualan',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'tr_item_bundling__payment_total_dpp',
      // query: 'tr_item_bundling.payment_total_dpp',
      query: `CASE WHEN tr_item.item_type != 'bundling' THEN tr_item.payment_total_dpp ELSE tr_item_bundling.payment_total_dpp END`,
      label: 'DPP',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'tr_item_bundling__payment_total_tax',
      // query: 'tr_item_bundling.payment_total_tax',
      query: `CASE WHEN tr_item.item_type != 'bundling' THEN tr_item.payment_total_tax ELSE tr_item_bundling.payment_total_tax END`,
      label: 'Total Pajak',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'tr_item_bundling__total_profit_share',
      // query: 'tr_item_bundling.total_profit_share',
      query: `CASE WHEN tr_item.item_type != 'bundling' THEN tr_item.total_profit_share ELSE tr_item_bundling.total_profit_share END`,
      label: 'Profit Share',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'tr_item_bundling__total_share_tenant',
      // query: 'tr_item_bundling.total_share_tenant',
      query: `CASE WHEN tr_item.item_type != 'bundling' THEN tr_item.total_share_tenant ELSE tr_item_bundling.total_share_tenant END`,
      label: 'Tenant Share',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'refund__refund_date',
      query: `to_char(refund.refund_date, 'DD-MM-YYYY')`,
      label: 'Tgl. Pengembalian',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'refund__status',
      query: 'refund.status',
      label: 'Status Pengembalian',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'refund__code',
      query: 'refund.code',
      label: 'Kode Pengembalian',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'refund_item__qty_refund',
      query: 'refund_item.qty_refund',
      label: 'Qty Pengembalian',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.NUMBER,
    },
    {
      column: 'main__customer_name',
      query: 'main.customer_name',
      label: 'Nama Pelanggan',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__customer_description',
      query: 'main.customer_description',
      label: 'Deskripsi',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__customer_phone',
      query: 'main.customer_phone',
      label: 'Telepon',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__creator_name',
      query: 'main.creator_name',
      label: 'Dibuat Oleh',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
  ],
  whereCondition(filterModel) {
    const queryFilter = [];
    const breakdown = filterModel.tr_item__breakdown_bundling;
    if (breakdown) {
      const value = breakdown.filter.map((item) => {
        return item === 'Yes' ? true : false;
      });

      queryFilter.push(`tr_item.breakdown_bundling in (${value.join()})`);
    }
    return queryFilter;
  },
  ignore_filter_keys: ['tr_item__breakdown_bundling'],
  filter_configs: [
    {
      filed_label: 'Tgl. Pendapatan',
      filter_column: 'main__payment_date',
      field_type: FILTER_FIELD_TYPE.date_range_picker,
      filter_type: FILTER_TYPE.DATE_IN_RANGE_TIMESTAMP,
      // date_format: 'DD-MM-YYYY',
      date_format: 'YYYY-MM-DD',
    },
    {
      filed_label: 'Kepemilikan',
      filter_column: 'item_owner',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Sumber',
      filter_column: 'main__type',
      field_type: FILTER_FIELD_TYPE.select,
      filter_type: FILTER_TYPE.TEXT_IN_MEMBER,
      select_custom_options: [...Object.values(TransactionType)],
    },
    {
      filed_label: 'Kode Booking',
      filter_column: 'main__invoice_code',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Kode Pembayaran',
      filter_column: 'main__payment_code',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Kategori Item',
      filter_column: 'tr_item__item_category_name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Nama Item',
      filter_column: 'tr_item__item_name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Tipe Item',
      filter_column: 'tr_item__item_type',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Nama Item Bundling',
      filter_column: 'tr_item_bundling__item_name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Time Group',
      filter_column: 'tg__name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Tipe Pelanggan',
      filter_column: 'main__customer_type',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'No. PoS',
      filter_column: 'main__creator_counter_no',
      field_type: FILTER_FIELD_TYPE.input_number,
      filter_type: FILTER_TYPE.NUMBER_EQUAL,
    },
    {
      filed_label: 'Nama PoS',
      filter_column: 'main__creator_counter_name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Tgl. Pengembalian',
      filter_column: 'refund__refund_date',
      field_type: FILTER_FIELD_TYPE.date_range_picker,
      filter_type: FILTER_TYPE.DATE_IN_RANGE_TIMESTAMP,
      // date_format: 'DD-MM-YYYY',
      date_format: 'YYYY-MM-DD',
    },
    {
      filed_label: 'Kode Pengembalian',
      filter_column: 'refund__code',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Nama Pelanggan',
      filter_column: 'main__customer_name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Bank/Issuer',
      filter_column: 'main__payment_type_method_name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Dibuat Oleh',
      filter_column: 'main__creator_name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
  ],
  customQueryColumn(column) {
    if (column === 'main__payment_date') return 'main.payment_date';
    else if (column === 'refund__refund_date') return 'refund.refund_date';
    return;
  },
};
