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}__reconciliation`,
  label: 'Rekonsiliasi',
  table_schema: `transactions main
    LEFT JOIN payment_methods payment ON payment.id = main.payment_type_method_id::uuid`,
  main_table_alias: 'main',
  defaultOrderBy: [],
  lowLevelOrderBy: [],
  filter_period_config: {
    hidden: true,
  },
  column_configs: [
    {
      column: 'main__reconciliation_status',
      query: 'main.reconciliation_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: '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__otp_code',
      query: 'main.otp_code',
      label: 'Kode OTP Reject',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__payment_date',
      query: `CASE WHEN main.payment_date is not null THEN to_char(main.payment_date, 'DD-MM-YYYY') ELSE null END`,
      label: 'Tgl. Transaksi',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__payment_date_bank',
      query: `CASE WHEN main.payment_date_bank is not null THEN to_char(main.payment_date_bank, 'DD-MM-YYYY') ELSE null END`,
      label: 'Tgl. Transaksi Bank',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__reconciliation_confirm_date',
      query: `CASE WHEN main.reconciliation_confirm_date is not null THEN to_char(cast(to_timestamp(COALESCE(main.reconciliation_confirm_date::NUMERIC)/1000) as date),'DD-MM-YYYY') ELSE null END`,
      label: 'Tgl. Konfirmasi',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__payment_code_reference',
      query: 'main.payment_code_reference',
      label: 'Referensi',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__payment_type',
      query: 'main.payment_type',
      label: 'Metode 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: 'payment__account_number',
      query: 'payment.account_number',
      label: 'Account No.',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__payment_total',
      query: 'main.payment_total',
      label: 'Total',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'main__reconciliation_mdr',
      query: 'main.reconciliation_mdr',
      label: 'MDR',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'main__payment_total_net_profit',
      query: 'main.payment_total_net_profit',
      label: 'Net Pendapatan',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'main__total_cash',
      query: `CASE WHEN main.payment_type ='cash' and main.reconciliation_status in('confirmed','pending') THEN  main.payment_total ELSE  0 END`,
      label: 'Total Tunai',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'main__total_transaction_bank',
      query: `CASE WHEN main.payment_type !='cash' and main.reconciliation_status ='confirmed' THEN  main.payment_total ELSE  0 END`,
      label: 'Total Transaksi Bank',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'main__total_outstanding',
      query: `CASE WHEN main.payment_type !='cash' and main.reconciliation_status ='pending' THEN  main.payment_total ELSE  0 END`,
      label: 'Total Outstanding',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'cashier',
      query: `CASE WHEN main.type = 'counter' THEN main.creator_name END`,
      label: 'Kasir',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'main__reconciliation_confirm_by',
      query: 'main.reconciliation_confirm_by',
      label: 'Dikonfirmasi Oleh',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
  ],
  filter_configs: [
    {
      filed_label: 'Status',
      filter_column: 'main__reconciliation_status',
      field_type: FILTER_FIELD_TYPE.select,
      filter_type: FILTER_TYPE.TEXT_IN_MEMBER,
      select_custom_options: [
        STATUS.PENDING,
        STATUS.CONFIRMED,
        STATUS.REJECTED,
      ],
    },
    {
      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: '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. Transaksi',
      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. Transaksi Bank',
      filter_column: 'main__payment_date_bank',
      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. Konfirmasi',
      filter_column: 'main__reconciliation_confirm_date',
      field_type: FILTER_FIELD_TYPE.date_range_picker,
      filter_type: FILTER_TYPE.DATE_IN_RANGE_TIMESTAMP,
      date_format: 'DD-MM-YYYY',
    },
    {
      filed_label: 'Referensi',
      filter_column: 'main__payment_code_reference',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Metode Pembayaran',
      filter_column: 'main__payment_type',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Issuer',
      filter_column: 'main__payment_type_method_name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Account No.',
      filter_column: 'payment__account_number',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Kasir',
      filter_column: 'cashier',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Dikonfirmasi Oleh',
      filter_column: 'main__reconciliation_confirm_by',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
  ],

  whereCondition(filterModel) {
    const statusFilter = filterModel?.main__reconciliation_status?.filter ?? [];
    if (statusFilter.length === 0) {
      return [`main.reconciliation_status NOT IN ('${STATUS.DRAFT}')`];
    }
    return [];
  },
  customQueryColumn(column) {
    if (column === 'main__payment_date') return 'main.payment_date';
    else if (column === 'main__payment_date_bank') {
      return 'main.payment_date_bank';
    }
    return;
  },
};
