import { SORT_ASC } from '@/modules/@common/constants/sort';

import {
  CHART_COMMON_DEFAULT_CONFIG,
  COMMON_COMPARISON_OPTIONS,
  COMMON_COMPARISON_PARAMS,
  COMMON_TIME_INTERVAL_OPTIONS,
  COMMON_TIME_INTERVAL_PARAMS,
  COMMON_TIME_INTERVAL_PARAMS_FOR_SELF_SERVE,
} from './chart';
import { DEMAND_TAG_PAGE_PATHNAME } from './demandTagPage';
import { STATUS_FILTERS } from './table';

export const DEMAND_ADVERTISER_PAGE = 'DEMAND_ADVERTISER_PAGE';

export const DEMAND_ADVERTISER_PAGE_CHANGE_AD_FEE_MODAL = 'DEMAND_ADVERTISER_PAGE_CHANGE_AD_FEE_MODAL';
export const DEMAND_ADVERTISER_PAGE_CHANGE_VIEWABILITY_THRESHOLD_MODAL =
  'DEMAND_ADVERTISER_PAGE_CHANGE_VIEWABILITY_THRESHOLD_MODAL';

export const DEMAND_ADVERTISER_PAGE_PATHNAME = '/advertisers';

export const DEMAND_ADVERTISER_PAGE_TABLE_HEADERS = [
  {
    id: 'NAME',
    label: 'Name',
    isSortable: true,
    defaultSortOrder: SORT_ASC,
  },
  {
    id: 'SUPPLIES',
    label: 'Supply Tags',
    isSortable: true,
  },
  {
    id: 'PRICING',
    label: 'Rate',
  },
  {
    id: 'targeting',
    label: 'Targeting',
    isSortable: false,
  },
  {
    id: 'REQUESTS',
    label: 'Requests',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'OPPORTUNITIES',
    label: 'Opportunities',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'IMPRESSIONS',
    label: 'Impressions',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'REVENUE',
    label: 'Revenue',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'GROSS_REVENUE',
    label: 'Gross Revenue',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'PROFIT',
    label: 'Profit',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'REQUESTS_FILL',
    label: 'Req Fill',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'OPPORTUNITIES_FILL',
    label: 'Opp Fill',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'ERPM',
    label: 'eRPM',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  {
    id: 'GROSS_ERPM',
    label: 'Gross eRPM',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  // only for admin
  {
    id: 'REQ_ERPM',
    label: 'Req eRPM',
    isSortable: true,
  },
  // only for admin
  {
    id: 'REQ_EPPM',
    label: 'Req ePPM',
    isSortable: true,
  },
  // only for admin
  {
    id: 'REQ_NET_ERPM',
    label: 'Req Pub NET eRPM',
    isSortable: true,
  },
  // only for self serve
  {
    id: 'REQ_GROSS_ERPM',
    label: 'Req Gross eRPM ',
    isSortable: true,
  },
  {
    id: 'CTR',
    label: 'CTR',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  {
    id: 'COMPLETION_RATE',
    label: 'Completion Rate',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  {
    id: 'CREATED',
    label: 'Creation Date',
    isSortable: true,
  },
  {
    id: 'COPY',
    label: '',
    isSortable: false,
  },
];

export const DEMAND_ADVERTISER_PAGE_TABLE_CELLS = [
  {
    key: 'name',
  },
  {
    key: 'supplies',
  },
  {
    key: 'pricing',
    prefix: '$',
    emptyMask: 'N/A',
  },
  {
    key: [
      'include',
      'exclude',
      'frequency',
      'advertiserInclude',
      'advertiserExclude',
      'waterfallSkip',
      'viewabilityThreshold',
      'kvTargeting',
    ],
  },
  {
    key: 'fields.REQUESTS',
    align: 'right',
    withPercent: true,
  },
  {
    key: 'fields.OPPORTUNITIES',
    align: 'right',
    withPercent: true,
  },
  {
    key: 'fields.IMPRESSIONS',
    align: 'right',
    withPercent: true,
  },
  {
    key: 'fields.REVENUE',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.GROSS_REVENUE',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.PROFIT',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.REQUESTS_FILL',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  {
    key: 'fields.OPPORTUNITIES_FILL',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  {
    key: 'fields.ERPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.GROSS_ERPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  // only for admin
  {
    key: 'fields.REQ_ERPM',
    align: 'right',
    prefix: '$',
  },
  // only for admin
  {
    key: 'fields.REQ_EPPM',
    align: 'right',
    prefix: '$',
  },
  // only for admin
  {
    key: 'fields.REQ_NET_ERPM',
    align: 'right',
    prefix: '$',
  },
  // only for self serve
  {
    key: 'fields.REQ_GROSS_ERPM',
    align: 'right',
    prefix: '$',
  },
  {
    key: 'fields.CTR',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  {
    key: 'fields.COMPLETION_RATE',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  {
    key: 'created',
  },
  {
    key: 'copy',
    align: 'left',
  },
];

export const DEMAND_ADVERTISER_PAGE_INFO_ROWS = [
  {
    label: 'Ad Requests',
    value: 'REQUESTS',
  },
  {
    label: 'Ad Impressions',
    value: 'IMPRESSIONS',
  },
  {
    label: 'Request Fill',
    value: 'REQUESTS_FILL',
    postfix: '%',
    needMultiply: true,
  },
  {
    label: 'Ad Opportunities',
    value: 'OPPORTUNITIES',
  },
  {
    label: 'Opportunities Fill',
    value: 'OPPORTUNITIES_FILL',
    postfix: '%',
    needMultiply: true,
  },
  {
    label: 'Revenue',
    value: 'REVENUE',
    prefix: '$',
  },
  {
    label: 'Gross Revenue',
    value: 'GROSS_REVENUE',
    prefix: '$',
  },
  {
    label: 'Profit',
    value: 'PROFIT',
    prefix: '$',
  },
  {
    label: 'RPM',
    value: 'RPM',
    prefix: '$',
  },
  {
    label: 'Gross RPM',
    value: 'GROSS_RPM',
    prefix: '$',
  },
  {
    label: 'eRPM',
    value: 'ERPM',
    prefix: '$',
  },
  {
    label: 'Gross eRPM',
    value: 'GROSS_ERPM',
    prefix: '$',
  },
  {
    label: 'Ad Clicks',
    value: 'CLICKS',
  },
  {
    label: 'CTR',
    value: 'CTR',
    postfix: '%',
    needMultiply: true,
  },
  {
    label: 'Completion Rate',
    value: 'COMPLETION_RATE',
    postfix: '%',
    needMultiply: true,
  },
  {
    label: 'Impression Viewability',
    value: 'IMPRESSIONS_VIEWABILITY',
    postfix: '%',
    needMultiply: true,
  },
];

export const DEMAND_ADVERTISER_PAGE_TOTAL_FIELDS = [
  'REQUESTS',
  'IMPRESSIONS',
  'REQUESTS_FILL',
  'OPPORTUNITIES',
  'OPPORTUNITIES_FILL',
  'REVENUE',
  'GROSS_REVENUE',
  'RPM',
  'GROSS_RPM',
  'ERPM',
  'GROSS_ERPM',
  'IMPRESSIONS_VIEWABILITY',
  'COMPLETION_RATE',
  'CTR',
  'CLICKS',
  'PROFIT',
];

export const DEMAND_ADVERTISER_PAGE_TOTAL_GRAPHQL_QUERY = `
  query AdvertiserDataByIdQuery(
    $fields: [String],
    $filters: MarketplaceFiltersInputType,
    $id: String!
  ) {
    advertiserDataById(
      fields: $fields,
      filters: $filters,
      id: $id,
    ) {
      fields {
        REQUESTS {
          value
          change
        }
        IMPRESSIONS {
          value
          change
        }
        REQUESTS_FILL {
          value
          change
        }
        OPPORTUNITIES {
          value
          change
        }
        OPPORTUNITIES_FILL {
          value
          change
        }
        REVENUE {
          value
          change
        }
        GROSS_REVENUE {
          value
          change
        }
        RPM {
          value
          change
        }
        GROSS_RPM {
          value
          change
        }
        ERPM {
          value
          change
        }
        GROSS_ERPM {
          value
          change
        }
        IMPRESSIONS_VIEWABILITY {
          value
          change
        }
        COMPLETION_RATE {
          value
          change
        }
        CTR {
          value
          change
        }
        CLICKS {
          value
          change
        }
        PROFIT {
          value
          change
        }
      }
    }
  }
`;

export const DEMAND_ADVERTISER_PAGE_DATA_FIELDS = [
  'REQUESTS',
  'OPPORTUNITIES',
  'IMPRESSIONS',
  'REQUESTS_FILL',
  'OPPORTUNITIES_FILL',
  'REVENUE',
  'GROSS_REVENUE',
  'ERPM',
  'GROSS_ERPM',
  'HB_RATE',
  'COMPLETION_RATE',
  'CTR',
  'REQ_ERPM',
  'REQ_EPPM',
  'REQ_NET_ERPM',
  'REQ_GROSS_ERPM',
  'PROFIT',
];

export const DEMAND_ADVERTISER_PAGE_DATA_GRAPHQL_QUERY = `
  query DemandTagDataQuery(
    $fields: [String],
    $sort: MarketplaceSortInputType,
    $from: Int,
    $size: Int,
    $filters: MarketplaceFiltersInputType,
    $allPages: Boolean,
    $advertiserTargeting: Boolean
  ) {
    demandTagData(
      fields: $fields,
      sort: $sort,
      from: $from,
      size: $size,
      filters: $filters,
      allPages: $allPages,
      advertiserTargeting: $advertiserTargeting
    ) {
      totalCount
      data {
        id
        name
        tier
        priority
        status
        supplies
        aps
        daccountId
        advertiserId
        created
        waterfallSkip
        viewabilityThreshold
        profit
        pricing {
          value
        }
        frequency {
          status
          value
          amount
          type
          timeframe
        }
        include {
          domains
          geo
          os
          browsers
          playerSizes
          viewability
          devices
        }
        exclude {
          domains
          geo
          os
          browsers
          devices
        }
        advertiserInclude {
          geo
        }
        advertiserExclude {
          geo
        }
        kvTargeting {
          state
          type
          key
          values
          listNames
          keyName
        }
        fields {
          REQUESTS {
            value
            change
          }
          OPPORTUNITIES {
            value
            change
          }
          IMPRESSIONS {
            value
            change
          }
          REQUESTS_FILL {
            value
            change
          }
          OPPORTUNITIES_FILL {
            value
            change
          }
          REVENUE {
            value
            change
          }
          GROSS_REVENUE {
            value
            change
          }
          ERPM {
            value
            change
          }
          GROSS_ERPM {
            value
            change
          }
          COMPLETION_RATE {
            value
            change
          }
          CTR {
            value
            change
          }
          REQ_ERPM {
            value
            change
          }
          REQ_EPPM {
            value
            change
          }
          REQ_NET_ERPM {
            value
            change
          }
          REQ_GROSS_ERPM {
            value
            change
          }
          PROFIT {
            value
            change
          }
        }
      }
    }
  }
`;

export const DEMAND_ADVERTISER_PAGE_HISTOGRAM_GRAPHQL_QUERY = `
  query DemandTagHistogramQuery(
    $fields: [String],
    $interval: String,
    $timezone: String,
    $filters: [MarketplaceFiltersInputType]
  ) {
    demandTagHistogram(
      fields: $fields,
      interval: $interval,
      timezone: $timezone,
      filters: $filters
    ) {
      totalCount
      data {
        name
        buckets {
          time
          fields {
            REQUESTS {
              value
              change
            }
            IMPRESSIONS {
              value
              change
            }
            REQUESTS_FILL {
              value
              change
            }
            REVENUE {
              value
              change
            }
            GROSS_REVENUE {
              value
              change
            }
            OPPORTUNITIES_FILL {
              value
              change
            }
          }
        }
      }
    }
  }
`;

export const DEMAND_ADVERTISER_PAGE_INFO_GRAPHQL_QUERY = `
  query AdvertiserByIdQuery(
    $id: String
  ) {
    advertiserById(
      id: $id
    ) {
      id
      name
      created
      updated
      updatedBy
      daccountId
      accountName
      tier
      publisherDemand
      pbsEnabled
      profitability
      frequencyCapAdjustment
      frequencyCapAdjustmentPerSupply
      frequencyCapAdjustmentThreshold
      include {
        geo
      }
      exclude {
        geo
      }
      revShare {
        value
        startDate
        endDate
        updatedBy
      }
      logo
    }
  }
`;

export const DEMAND_ADVERTISER_PAGE_CONFIG_TAB_SETTINGS_ID = 'advertiserSettings';
export const DEMAND_ADVERTISER_PAGE_CONFIG = {
  type: DEMAND_ADVERTISER_PAGE,
  tabs: [
    {
      label: 'Demand Tags',
      dataId: 'main',
      contentKey: 'main',
    },
    {
      label: 'Settings',
      dataId: DEMAND_ADVERTISER_PAGE_CONFIG_TAB_SETTINGS_ID,
      contentKey: DEMAND_ADVERTISER_PAGE_CONFIG_TAB_SETTINGS_ID,
    },
    {
      label: 'Pricing',
      dataId: 'advertiserPricing',
      contentKey: 'advertiserPricing',
    },
  ],
  targetingTabConfig: {
    name: true,
    geography: true,
    geographyTooltip: true,
    tier: true,
  },
  nextLevelLink: DEMAND_TAG_PAGE_PATHNAME,
  nextLevelQuery: 'tagName',
  infoRows: DEMAND_ADVERTISER_PAGE_INFO_ROWS,
  rowsInColumn: 10,
  tableSelecting: true,
  tableHeaders: DEMAND_ADVERTISER_PAGE_TABLE_HEADERS.filter((item) => !['REQ_GROSS_ERPM'].includes(item.id)),
  tableCells: DEMAND_ADVERTISER_PAGE_TABLE_CELLS.filter((item) => !['fields.REQ_GROSS_ERPM'].includes(item.key)),
  tableFilters: STATUS_FILTERS,
  showFilterByLabel: true,
  timeIntervalOptions: COMMON_TIME_INTERVAL_OPTIONS,
  timeIntervalParams: COMMON_TIME_INTERVAL_PARAMS,
  comparisonOptions: [
    ...COMMON_COMPARISON_OPTIONS,
    {
      label: 'Opportunities Fill',
      value: 'OPPORTUNITIES_FILL',
    },
  ],
  comparisonParams: COMMON_COMPARISON_PARAMS,
  rowsPerPageOptions: [10, 25, 50, 100, 200],
  // defaultConfig for Admin MP
  defaultConfig: {
    ...CHART_COMMON_DEFAULT_CONFIG,
    filterByStatus: STATUS_FILTERS[1].value,
  },
  totalFields: DEMAND_ADVERTISER_PAGE_TOTAL_FIELDS,
  totalQuery: DEMAND_ADVERTISER_PAGE_TOTAL_GRAPHQL_QUERY,
  dataFields: DEMAND_ADVERTISER_PAGE_DATA_FIELDS,
  dataQuery: DEMAND_ADVERTISER_PAGE_DATA_GRAPHQL_QUERY,
  histogramQuery: DEMAND_ADVERTISER_PAGE_HISTOGRAM_GRAPHQL_QUERY,
  infoQuery: DEMAND_ADVERTISER_PAGE_INFO_GRAPHQL_QUERY,
  selfServeConfig: {
    showFilterByLabel: false,
    infoRows: DEMAND_ADVERTISER_PAGE_INFO_ROWS.filter(
      (item) => !['REVENUE', 'RPM', 'PROFIT', 'ERPM'].includes(item.value),
    ).map((item) => {
      if (item.value === 'GROSS_REVENUE') {
        return { ...item, label: 'Pub Revenue' };
      }
      if (item.value === 'GROSS_RPM') {
        return { ...item, label: 'Ad RPM' };
      }
      if (item.value === 'GROSS_ERPM') {
        return { ...item, label: 'Opp eRPM' };
      }
      return item;
    }),
    timeIntervalParams: COMMON_TIME_INTERVAL_PARAMS_FOR_SELF_SERVE,
    comparisonOptions: [
      ...COMMON_COMPARISON_OPTIONS.filter((item) => !['REVENUE'].includes(item.value)).map((item) => {
        if (item.value === 'GROSS_REVENUE') {
          return { ...item, label: 'Pub Revenue' };
        }
        return item;
      }),
      {
        label: 'Opportunities Fill',
        value: 'OPPORTUNITIES_FILL',
      },
    ],
    tableHeaders: DEMAND_ADVERTISER_PAGE_TABLE_HEADERS.filter(
      (item) => !['REVENUE', 'PROFIT', 'ERPM', 'REQ_ERPM', 'REQ_EPPM', 'REQ_NET_ERPM'].includes(item.id),
    ).map((item) => {
      if (item.id === 'GROSS_REVENUE') {
        return { ...item, label: 'Pub Revenue' };
      }
      if (item.id === 'GROSS_ERPM') {
        return { ...item, label: 'Ad RPM' };
      }
      if (item.id === 'REQ_GROSS_ERPM') {
        return { ...item, label: 'Req eRPM' };
      }
      return item;
    }),
    tableCells: DEMAND_ADVERTISER_PAGE_TABLE_CELLS.filter(
      (item) =>
        ![
          'fields.REVENUE',
          'fields.PROFIT',
          'fields.ERPM',
          'fields.REQ_ERPM',
          'fields.REQ_EPPM',
          'fields.REQ_NET_ERPM',
        ].includes(item.key),
    ),
    // defaultConfig for Self Serve MP
    defaultConfig: {
      comparisonFilter: COMMON_COMPARISON_OPTIONS[0].value,
    },
  },
  showDownloadCSVButton: true,
};
