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

import {
  CHART_COMMON_DEFAULT_CONFIG,
  COMMON_COMPARISON_PARAMS,
  COMMON_SUPPLY_COMPARISON_OPTIONS,
  COMMON_SUPPLY_TIME_INTERVAL_PARAMS,
  COMMON_TIME_INTERVAL_OPTIONS,
} from './chart';
import { SUPPLY_SITE_PAGE_PATHNAME } from './supplySitePage';
import { STATUS_FILTERS } from './table';

export const SUPPLY_ACCOUNT_PAGE = 'SUPPLY_ACCOUNT_PAGE';

export const SUPPLY_ACCOUNT_PAGE_PATHNAME = '/supply';

export const SUPPLY_ACCOUNT_PAGE_TABLE_HEADERS = [
  {
    id: 'ID',
    label: 'Id',
    isSortable: true,
  },
  {
    id: 'NAME',
    label: 'Name',
    isSortable: true,
    defaultSortOrder: SORT_ASC,
  },
  {
    id: 'SUPPLIES',
    label: 'Supply Tags',
    isSortable: true,
  },
  {
    id: 'PLAYER_LOADS',
    label: 'Player Loads',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'SUPPLY_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: 'PUB_NET_REVENUE',
    label: 'Pub NET Revenue',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'SUPPLY_REQUESTS_FILL',
    label: 'Req Fill',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'OVERALL_FILL',
    label: 'Overall Fill',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'PUB_PLAYER_ERPM',
    label: 'Player eRPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'PLAYER_ERPM',
    label: 'Player eRPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'GROSS_PLAYER_ERPM',
    label: 'Gross Player eRPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'PUB_NET_PLAYER_ERPM',
    label: 'Pub NET Player eRPM',
    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,
  },
];

export const SUPPLY_ACCOUNT_PAGE_TABLE_CELLS = [
  {
    key: 'id',
  },
  {
    key: 'name',
  },
  {
    key: 'supplies',
  },
  {
    key: 'fields.PLAYER_LOADS',
    align: 'right',
    withPercent: true,
  },
  {
    key: 'fields.SUPPLY_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.PUB_NET_REVENUE',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.SUPPLY_REQUESTS_FILL',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  {
    key: 'fields.OVERALL_FILL',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  {
    key: 'fields.PUB_PLAYER_ERPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.PLAYER_ERPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.GROSS_PLAYER_ERPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.PUB_NET_PLAYER_ERPM',
    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,
  },
];

export const SUPPLY_ACCOUNT_PAGE_INFO_ROWS = [
  {
    label: 'Player Loads',
    value: 'PLAYER_LOADS',
  },
  {
    label: 'Ad Requests',
    value: 'SUPPLY_REQUESTS',
  },
  {
    label: 'Ad Impressions',
    value: 'IMPRESSIONS',
  },
  {
    label: 'Request Fill',
    value: 'SUPPLY_REQUESTS_FILL',
    postfix: '%',
    needMultiply: true,
  },
  {
    label: 'Overall Fill',
    value: 'OVERALL_FILL',
    postfix: '%',
    needMultiply: true,
  },
  {
    label: 'Revenue',
    value: 'REVENUE',
    prefix: '$',
  },
  {
    label: 'Gross Revenue',
    value: 'GROSS_REVENUE',
    prefix: '$',
  },
  {
    label: 'Pub NET Revenue',
    value: 'PUB_NET_REVENUE',
    prefix: '$',
  },
  {
    label: 'Expenses',
    value: 'EXPENSES',
    prefix: '$',
  },
  {
    label: 'Media Cost',
    value: 'MEDIA_COST',
    prefix: '$',
  },
  {
    label: 'Media Margin',
    value: 'MEDIA_MARGIN',
    prefix: '$',
  },
  {
    label: 'AC Ad RPM',
    value: 'AC_AD_RPM',
    prefix: '$',
  },
  {
    label: 'Gross Ad RPM',
    value: 'GROSS_RPM',
    prefix: '$',
  },
  {
    label: 'Ad RPM',
    value: 'PUB_AD_RPM',
    prefix: '$',
  },
  {
    label: 'CPM',
    value: 'CPM',
    prefix: '$',
  },
  {
    label: 'Player eRPM',
    value: 'PUB_PLAYER_ERPM',
    prefix: '$',
  },
  {
    label: 'Player eRPM',
    value: 'PLAYER_ERPM',
    prefix: '$',
  },
  {
    label: 'Gross Player eRPM',
    value: 'GROSS_PLAYER_ERPM',
    prefix: '$',
  },
  {
    label: 'Pub NET Player eRPM',
    value: 'PUB_NET_PLAYER_ERPM',
    prefix: '$',
  },
];

export const SUPPLY_ACCOUNT_PAGE_TOTAL_FIELDS = [
  'PLAYER_LOADS',
  'SUPPLY_REQUESTS',
  'IMPRESSIONS',
  'SUPPLY_REQUESTS_FILL',
  'OVERALL_FILL',
  'REVENUE',
  'GROSS_REVENUE',
  'MEDIA_COST',
  'AC_AD_RPM',
  'GROSS_RPM',
  'PUB_AD_RPM',
  'CPM',
  'PUB_PLAYER_ERPM',
  'PLAYER_ERPM',
  'GROSS_PLAYER_ERPM',
  'MEDIA_MARGIN',
  'EXPENSES',
  'PUB_NET_REVENUE',
  'PUB_NET_PLAYER_ERPM',
];

export const SUPPLY_ACCOUNT_PAGE_TOTAL_GRAPHQL_QUERY = `
  query SupplyAccountDataByIdQuery(
    $fields: [String],
    $filters: MarketplaceFiltersInputType,
    $id: String!
  ) {
    supplyAccountDataById(
      fields: $fields,
      filters: $filters,
      id: $id
    ) {
      fields {
        PLAYER_LOADS {
          value
          change
        }
        SUPPLY_REQUESTS {
          value
          change
        }
        IMPRESSIONS {
          value
          change
        }
        SUPPLY_REQUESTS_FILL {
          value
          change
        }
        OVERALL_FILL {
          value
          change
        }
        REVENUE {
          value
          change
        }
        GROSS_REVENUE {
          value
          change
        }
        MEDIA_COST {
          value
          change
        }
        AC_AD_RPM {
          value
          change
        }
        GROSS_RPM {
          value
          change
        }
        PUB_AD_RPM {
          value
          change
        }
        CPM {
          value
          change
        }
        PUB_PLAYER_ERPM {
          value
          change
        }
        PLAYER_ERPM {
          value
          change
        }
        GROSS_PLAYER_ERPM {
          value
          change
        }
        MEDIA_MARGIN {
          value
          change
        }
        EXPENSES {
          value
          change
        }
        PUB_NET_REVENUE {
          value
          change
        }
        PUB_NET_PLAYER_ERPM {
          value
          change
        }
      }
    }
  }
`;

export const SUPPLY_ACCOUNT_PAGE_DATA_FIELDS = [
  'PAGE_VIEWS',
  'PLAYER_LOADS',
  'SUPPLY_REQUESTS',
  'IMPRESSIONS',
  'SUPPLY_REQUESTS_FILL',
  'OVERALL_FILL',
  'REVENUE',
  'GROSS_REVENUE',
  'PUB_PLAYER_ERPM',
  'PLAYER_ERPM',
  'GROSS_PLAYER_ERPM',
  'COMPLETION_RATE',
  'CTR',
  'PUB_NET_REVENUE',
  'PUB_NET_PLAYER_ERPM',
];

export const SUPPLY_ACCOUNT_PAGE_DATA_GRAPHQL_QUERY = `
  query SiteDataQuery(
    $fields: [String],
    $sort: MarketplaceSortInputType,
    $from: Int,
    $size: Int,
    $filters: MarketplaceFiltersInputType,
    $allPages: Boolean
  ) {
    siteData(
      fields: $fields,
      sort: $sort,
      from: $from,
      size: $size,
      filters: $filters,
      allPages: $allPages
    ) {
      totalCount
      data {
        id
        name
        supplies
        fields {
          PLAYER_LOADS {
            value
            change
          }
          SUPPLY_REQUESTS {
            value
            change
          }
          IMPRESSIONS {
            value
            change
          }
          SUPPLY_REQUESTS_FILL {
            value
            change
          }
          OVERALL_FILL {
            value
            change
          }
          REVENUE {
            value
            change
          }
          GROSS_REVENUE {
            value
            change
          }
          PUB_PLAYER_ERPM {
            value
            change
          }
          PLAYER_ERPM {
            value
            change
          }
          GROSS_PLAYER_ERPM {
            value
            change
          }
          COMPLETION_RATE {
            value
            change
          }
          CTR {
            value
            change
          }
          PUB_NET_REVENUE {
            value
            change
          }
          PUB_NET_PLAYER_ERPM {
            value
            change
          }
        }
      }
    }
  }
`;

export const SUPPLY_ACCOUNT_PAGE_HISTOGRAM_GRAPHQL_QUERY = `
  query SiteHistogramQuery(
    $fields: [String],
    $interval: String,
    $timezone: String,
    $filters: [MarketplaceFiltersInputType]
  ) {
    siteHistogram(
      fields: $fields,
      interval: $interval,
      timezone: $timezone,
      filters: $filters
    ) {
      totalCount
      data {
        name
        buckets {
          time
          fields {
            SUPPLY_REQUESTS {
              value
              change
            }
            IMPRESSIONS {
              value
              change
            }
            SUPPLY_REQUESTS_FILL {
              value
              change
            }
            REVENUE {
              value
              change
            }
            GROSS_REVENUE {
              value
              change
            }
            PLAYER_LOADS {
              value
              change
            }
            OVERALL_FILL {
              value
              change
            }
            PUB_PLAYER_ERPM {
              value
              change
            }
            PLAYER_ERPM {
              value
              change
            }
            GROSS_PLAYER_ERPM {
              value
              change
            }
            SUPPLY_REQUESTS_VIEWABILITY {
              value
              change
            }
            IMPRESSIONS_VIEWABILITY {
              value
              change
            }
            AC_AD_RPM {
              value
              change
            }
            GROSS_RPM {
              value
              change
            }
            PUB_AD_RPM {
              value
              change
            }
            PUB_NET_REVENUE {
              value
              change
            }
            PUB_NET_PLAYER_ERPM {
              value
              change
            }
          }
        }
      }
    }
  }
`;

export const SUPPLY_ACCOUNT_PAGE_INFO_GRAPHQL_QUERY = `
  query SupplyAccountByIdQuery(
    $id: String
  ) {
    supplyAccountById(
      id: $id
    ) {
      id
      name
      created
      updated
      updatedBy
    }
  }
`;

const comparisonOptions = [
  ...COMMON_SUPPLY_COMPARISON_OPTIONS,
  {
    label: 'Player Loads',
    value: 'PLAYER_LOADS',
  },
  {
    label: 'Overall Fill ',
    value: 'OVERALL_FILL',
  },
  {
    label: 'AC Ad RPM',
    value: 'AC_AD_RPM',
  },
  {
    label: 'Gross Ad RPM',
    value: 'GROSS_RPM',
  },
  {
    label: 'Ad RPM',
    value: 'PUB_AD_RPM',
  },
  {
    label: 'Player eRPM',
    value: 'PLAYER_ERPM',
  },
  {
    label: 'Gross Player eRPM',
    value: 'GROSS_PLAYER_ERPM',
  },
  {
    label: 'Pub NET Player eRPM',
    value: 'PUB_NET_PLAYER_ERPM',
  },
  {
    label: 'Request Viewability',
    value: 'SUPPLY_REQUESTS_VIEWABILITY',
  },
  {
    label: 'Impression Viewability',
    value: 'IMPRESSIONS_VIEWABILITY',
  },
];

export const SUPPLY_ACCOUNT_PAGE_CONFIG = {
  type: SUPPLY_ACCOUNT_PAGE,
  tabs: [
    {
      label: 'Sites',
      dataId: 'main',
      contentKey: 'main',
    },
  ],
  nextLevelLink: SUPPLY_SITE_PAGE_PATHNAME,
  nextLevelQuery: 'siteName',
  infoRows: SUPPLY_ACCOUNT_PAGE_INFO_ROWS.filter((item) => !['PUB_PLAYER_ERPM', 'PUB_AD_RPM'].includes(item.value)),
  rowsInColumn: 11,
  tableSelecting: false,
  tableFilters: STATUS_FILTERS,
  tableHeaders: SUPPLY_ACCOUNT_PAGE_TABLE_HEADERS.filter(
    (item) => !['PUB_PLAYER_ERPM', 'PUB_AD_RPM'].includes(item.id),
  ),
  tableCells: SUPPLY_ACCOUNT_PAGE_TABLE_CELLS.filter(
    (item) => !['fields.PUB_PLAYER_ERPM', 'fields.PUB_AD_RPM'].includes(item.key),
  ),
  timeIntervalOptions: COMMON_TIME_INTERVAL_OPTIONS,
  timeIntervalParams: COMMON_SUPPLY_TIME_INTERVAL_PARAMS,
  comparisonOptions: comparisonOptions.filter((item) => !['PUB_AD_RPM'].includes(item.value)),
  comparisonParams: COMMON_COMPARISON_PARAMS,
  rowsPerPageOptions: [10, 25, 50, 100, 200],
  defaultConfig: {
    ...CHART_COMMON_DEFAULT_CONFIG,
    sortBy: 'SUPPLY_REQUESTS',
    filterByStatus: STATUS_FILTERS[1].value,
  },
  totalFields: SUPPLY_ACCOUNT_PAGE_TOTAL_FIELDS,
  totalQuery: SUPPLY_ACCOUNT_PAGE_TOTAL_GRAPHQL_QUERY,
  dataFields: SUPPLY_ACCOUNT_PAGE_DATA_FIELDS,
  dataQuery: SUPPLY_ACCOUNT_PAGE_DATA_GRAPHQL_QUERY,
  histogramQuery: SUPPLY_ACCOUNT_PAGE_HISTOGRAM_GRAPHQL_QUERY,
  infoQuery: SUPPLY_ACCOUNT_PAGE_INFO_GRAPHQL_QUERY,
  showDownloadCSVButton: true,
  selfServeConfig: {
    infoRows: SUPPLY_ACCOUNT_PAGE_INFO_ROWS.filter((item) => !['PLAYER_ERPM', 'GROSS_RPM'].includes(item.value)),
    tableHeaders: SUPPLY_ACCOUNT_PAGE_TABLE_HEADERS.filter((item) => !['PLAYER_ERPM', 'GROSS_RPM'].includes(item.id)),
    tableCells: SUPPLY_ACCOUNT_PAGE_TABLE_CELLS.filter(
      (item) => !['fields.PLAYER_ERPM', 'fields.GROSS_RPM'].includes(item.key),
    ),
    comparisonOptions: comparisonOptions.filter((item) => !['PUB_PLAYER_ERPM', 'GROSS_RPM'].includes(item.value)),
  },
};
