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}__cancel`,
  label: 'Pembatalan',
  table_schema: `transactions main
    LEFT JOIN season_types s_period_type ON s_period_type.id = main.season_period_type_id::uuid
    LEFT JOIN refunds refund ON refund.transaction_id::uuid = main.id and refund.status != 'cancel'
    LEFT JOIN vip_codes vip ON vip.id = main.discount_code_id::uuid
    LEFT JOIN (
      select item.transaction_id, sum(item.total_hpp) AS total_hpp_item
      from transaction_items item
      group by  item.transaction_id
    ) item ON item.transaction_id = main.id`,
  main_table_alias: 'main',
  whereDefaultConditions: [
    {
      column: 'main.status',
      filter_type: FILTER_TYPE.TEXT_IN_MEMBER,
      values: [STATUS.CANCEL],
    },
    {
      column: 'main.is_recap_transaction',
      filter_type: FILTER_TYPE.TEXT_EQUAL,
      values: [false],
    },
  ],
  defaultOrderBy: [],
  lowLevelOrderBy: [],
  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__payment_date',
      query: `to_char(cast(to_timestamp(main.created_at/1000) as 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: 'main__type',
      query: 'main.type',
      label: 'Sumber',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 's_period_type__name',
      query: 's_period_type.name',
      label: 'Tipe Rate',
      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__otp_code',
      query: 'main.otp_code',
      label: 'Kode OTP',
      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: '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: 'main__no_of_group',
      query: 'main.no_of_group::NUMERIC',
      label: '#Visitor',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.NUMBER,
    },
    {
      column: 'item__total_hpp_item',
      query: 'item.total_hpp_item',
      label: 'Total HPP',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'main__payment_sub_total',
      query: 'main.payment_sub_total',
      label: 'Subtotal',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'main__discount_percentage',
      query: 'main.discount_percentage',
      label: 'Diskon (%)',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.NUMBER,
    },
    {
      column: 'main__discount_value',
      query: 'main.discount_value',
      label: 'Diskon (IDR)',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'main__payment_total',
      query: 'main.payment_total',
      label: 'Total Penjualan',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'main__payment_total_dpp',
      query: 'main.payment_total_dpp',
      label: 'DPP',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'main__payment_total_tax',
      query: 'main.payment_total_tax',
      label: 'Total Pajak',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'main__payment_total_share',
      query: 'main.payment_total_share',
      label: 'Profit 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__refund_total',
    //   query: '(refund.refund_total * -1)',
    //   label: 'Total Pengembalian',
    //   type: DATA_TYPE.MEASURE,
    //   format: DATA_FORMAT.CURRENCY,
    // },
    // {
    //   column: 'transaction_balance',
    //   query: `CASE WHEN refund.id  is null THEN main.payment_total ELSE main.payment_total - refund.refund_total END`,
    //   label: 'Balance',
    //   type: DATA_TYPE.MEASURE,
    //   format: DATA_FORMAT.CURRENCY,
    // },
    {
      column: 'main__discount_code',
      query: 'main.discount_code',
      label: 'Kode Diskon',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'vip__creator_name',
      query: 'vip.creator_name',
      label: 'Diberikan Oleh',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      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__payment_type_counter',
      query: 'main.payment_type_counter',
      label: 'Type Pembayaran',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__payment_type_method_name',
      query: 'main.payment_type_method_name',
      label: 'Bank/Issuer',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__payment_card_information',
      query: 'main.payment_card_information',
      label: 'Information',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__creator_name',
      query: 'main.creator_name',
      label: 'Penjualan Dibuat Oleh',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__reconciliation_confirm_by',
      query: 'main.reconciliation_confirm_by',
      label: 'Direkonsiliasi Oleh',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'refund__creator_name',
      query: 'refund.creator_name',
      label: 'Pengembalian Dibuat Oleh',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
  ],
  filter_configs: [
    // {
    //   filed_label: 'Tgl. Pembatalan',
    //   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: 'Tgl. Pembatalan',
      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',
    },
    {
      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: 'Tipe Rate',
      filter_column: 's_period_type__name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      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: '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: 'Kode Diskon',
      filter_column: 'main__discount_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: 'Type Pembayaran',
      filter_column: 'main__payment_type_counter',
      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: 'Penjualan Dibuat Oleh',
      filter_column: 'main__creator_name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Direkonsiliasi Oleh',
      filter_column: 'main__reconciliation_confirm_by',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Pengembalian Dibuat Oleh',
      filter_column: 'refund__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';
    if (column === 'refund__refund_date') return 'refund.refund_date';
    return;
  },
};
