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}__giving_discounts`,
  label: 'Pemberian Diskon',
  table_schema: `transactions main
    LEFT JOIN vip_codes vip ON vip.id = main.discount_code_id::uuid
    LEFT JOIN vip_categories vip_category ON vip_category.id = vip.vip_category_id
    LEFT JOIN users account ON account.id = vip.creator_id::uuid
    LEFT JOIN user_privileges privilege ON privilege.id = account.user_privilege_id
    LEFT JOIN season_types s_period_type ON s_period_type.id = main.season_period_type_id::uuid`,
  main_table_alias: 'main',
  whereDefaultConditions: [
    {
      column: 'main.status',
      filter_type: FILTER_TYPE.TEXT_IN_MEMBER,
      values: [STATUS.SETTLED],
    },
    {
      column: 'main.type',
      filter_type: FILTER_TYPE.TEXT_IN_MEMBER,
      values: [TransactionType.COUNTER],
    },
    {
      column: 'main.is_recap_transaction',
      filter_type: FILTER_TYPE.TEXT_EQUAL,
      values: [false],
    },
  ],
  whereCondition(_filterModel) {
    return [
      `main.discount_percentage is not null or main.discount_value is not null`,
    ];
  },
  defaultOrderBy: [],
  lowLevelOrderBy: [],
  filter_period_config: {
    hidden: true,
  },

  column_configs: [
    {
      column: 'main__payment_date',
      query: `to_char(main.payment_date, 'DD-MM-YYYY')`,
      label: 'Tgl. Transaksi',
      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__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__payment_sub_total',
      query: 'main.payment_sub_total',
      label: 'Total Transaksi',
      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: 'main__discount_percentage',
      query: 'main.discount_percentage',
      label: 'Diskon (%)',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      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 Pembayaran',
      type: DATA_TYPE.MEASURE,
      format: DATA_FORMAT.CURRENCY,
    },
    {
      column: 'vip__creator_name',
      query: 'vip.creator_name',
      label: 'Pemberi Diskon',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
    {
      column: 'vip__otp_code',
      query: 'vip.otp_code',
      label: 'Kode OTP',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },

    {
      column: 'privilege__name',
      query: 'privilege.name',
      label: 'Kategori Pemberi Diskon',
      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__customer_name',
      query: 'main.customer_name',
      label: 'Nama Pelanggan',
      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: 'Kasir',
      type: DATA_TYPE.DIMENSION,
      format: DATA_FORMAT.TEXT,
    },
  ],
  filter_configs: [
    {
      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: '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 Transaksi',
      filter_column: 'main__invoice_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: 'Pemberi Diskon',
      filter_column: 'vip__creator_name',
      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 Pelanggan',
      filter_column: 'main__customer_name',
      field_type: FILTER_FIELD_TYPE.input_tag,
      filter_type: FILTER_TYPE.TEXT_MULTIPLE_CONTAINS,
    },
    {
      filed_label: 'Kasir',
      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';
    return;
  },
};
