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,
} from './chart';
import { DEMAND_ADVERTISER_PAGE_PATHNAME } from './demandAdvertiserPage';
import { STATUS_FILTERS } from './table';

export const DEMAND_ACCOUNT_PAGE = 'DEMAND_ACCOUNT_PAGE';

export const DEMAND_ACCOUNT_PAGE_PATHNAME = '/demand';

export const DEMAND_ACCOUNT_PAGE_TABLE_HEADERS = [
  {
    id: 'NAME',
    label: 'Name',
    isSortable: true,
    defaultSortOrder: SORT_ASC,
  },
  {
    id: 'DEMANDS',
    label: 'Demand Tags',
    isSortable: true,
  },
  {
    id: 'REQUESTS',
    label: 'Requests',
    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: 'REQUESTS_FILL',
    label: 'Req Fill',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'RPM',
    label: 'RPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'GROSS_RPM',
    label: 'Gross RPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'CTR',
    label: 'CTR',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  {
    id: 'COMPLETION_RATE',
    label: 'Completion Rate',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  {
    id: 'ERPM',
    label: 'eRPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'GROSS_ERPM',
    label: 'Gross eRPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
];

export const DEMAND_ACCOUNT_PAGE_TABLE_CELLS = [
  {
    key: 'name',
  },
  {
    key: 'demands',
  },
  {
    key: 'fields.REQUESTS',
    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.REQUESTS_FILL',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  {
    key: 'fields.RPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.GROSS_RPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.CTR',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  {
    key: 'fields.COMPLETION_RATE',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  {
    key: 'fields.ERPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.GROSS_ERPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
];

export const DEMAND_ACCOUNT_PAGE_INFO_ROWS = [
  {
    label: 'Ad Requests',
    value: 'REQUESTS',
  },
  {
    label: 'Ad Impressions',
    value: 'IMPRESSIONS',
  },
  {
    label: 'Request Fill',
    value: 'REQUESTS_FILL',
    postfix: '%',
    needMultiply: true,
  },
  {
    label: 'Revenue',
    value: 'REVENUE',
    prefix: '$',
  },
  {
    label: 'Gross Revenue',
    value: 'GROSS_REVENUE',
    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: 'Impression Viewability',
    value: 'IMPRESSIONS_VIEWABILITY',
    postfix: '%',
    needMultiply: true,
  },
];

export const DEMAND_ACCOUNT_PAGE_TOTAL_FIELDS = [
  'REQUESTS',
  'IMPRESSIONS',
  'REQUESTS_FILL',
  'REVENUE',
  'GROSS_REVENUE',
  'RPM',
  'GROSS_RPM',
  'ERPM',
  'GROSS_ERPM',
  'IMPRESSIONS_VIEWABILITY',
];

export const DEMAND_ACCOUNT_PAGE_TOTAL_GRAPHQL_QUERY = `
  query DemandAccountDataByIdQuery(
    $fields: [String],
    $filters: MarketplaceFiltersInputType,
    $id: String!
  ) {
    demandAccountDataById(
      fields: $fields,
      filters: $filters,
      id: $id
    ) {
      fields {
        REQUESTS {
          value
          change
        }
        IMPRESSIONS {
          value
          change
        }
        REQUESTS_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
        }
      }
    }
  }
`;

export const DEMAND_ACCOUNT_PAGE_DATA_FIELDS = [
  'REQUESTS',
  'IMPRESSIONS',
  'REQUESTS_FILL',
  'REVENUE',
  'GROSS_REVENUE',
  'RPM',
  'GROSS_RPM',
  'ERPM',
  'GROSS_ERPM',
  'COMPLETION_RATE',
  'CTR',
];

export const DEMAND_ACCOUNT_PAGE_DATA_GRAPHQL_QUERY = `
  query AdvertiserDataQuery(
    $fields: [String],
    $sort: MarketplaceSortInputType,
    $from: Int,
    $size: Int,
    $filters: MarketplaceFiltersInputType,
    $allPages: Boolean
  ) {
    advertiserData(
      fields: $fields,
      sort: $sort,
      from: $from,
      size: $size,
      filters: $filters,
      allPages: $allPages
    ) {
      totalCount
      data {
        id
        name
        demands
        hasApsTags
        fields {
          REQUESTS {
            value
            change
          }
          IMPRESSIONS {
            value
            change
          }
          REQUESTS_FILL {
            value
            change
          }
          REVENUE {
            value
            change
          }
          GROSS_REVENUE {
            value
            change
          }
          RPM {
            value
            change
          }
          GROSS_RPM {
            value
            change
          }
          ERPM {
            value
            change
          }
          GROSS_ERPM {
            value
            change
          }
          COMPLETION_RATE {
            value
            change
          }
          CTR {
            value
            change
          }
        }
      }
    }
  }
`;

export const DEMAND_ACCOUNT_PAGE_HISTOGRAM_GRAPHQL_QUERY = `
  query AdvertiserHistogramQuery(
    $fields: [String],
    $interval: String,
    $timezone: String,
    $filters: [MarketplaceFiltersInputType]
  ) {
    advertiserHistogram(
      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_ACCOUNT_PAGE_INFO_GRAPHQL_QUERY = `
  query DemandAccountByIdQuery(
    $id: String
  ) {
    demandAccountById(
      id: $id
    ) {
      id
      name
      created
      updated
      updatedBy
      defaultValues {
        videoAdServingFee
        displayAdServingFee
        adServingModel
      }
    }
  }
`;

export const DEMAND_ACCOUNT_PAGE_CONFIG = {
  type: DEMAND_ACCOUNT_PAGE,
  tabs: [
    {
      label: 'Advertisers',
      dataId: 'main',
      contentKey: 'main',
    },
  ],
  nextLevelLink: DEMAND_ADVERTISER_PAGE_PATHNAME,
  nextLevelQuery: 'advertiserName',
  infoRows: DEMAND_ACCOUNT_PAGE_INFO_ROWS,
  tableSelecting: false,
  tableFilters: STATUS_FILTERS,
  tableHeaders: DEMAND_ACCOUNT_PAGE_TABLE_HEADERS,
  tableCells: DEMAND_ACCOUNT_PAGE_TABLE_CELLS,
  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: {
    ...CHART_COMMON_DEFAULT_CONFIG,
    filterByStatus: STATUS_FILTERS[1].value,
  },
  totalFields: DEMAND_ACCOUNT_PAGE_TOTAL_FIELDS,
  totalQuery: DEMAND_ACCOUNT_PAGE_TOTAL_GRAPHQL_QUERY,
  dataFields: DEMAND_ACCOUNT_PAGE_DATA_FIELDS,
  dataQuery: DEMAND_ACCOUNT_PAGE_DATA_GRAPHQL_QUERY,
  histogramQuery: DEMAND_ACCOUNT_PAGE_HISTOGRAM_GRAPHQL_QUERY,
  infoQuery: DEMAND_ACCOUNT_PAGE_INFO_GRAPHQL_QUERY,
  showDownloadCSVButton: true,
};
