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_TAG_PAGE_PATHNAME } from './supplyTagPage';
import { STATUS_FILTERS } from './table';

export const SUPPLY_SITE_PAGE = 'SUPPLY_SITE_PAGE';

export const SUPPLY_SITE_PAGE_PATHNAME = '/sites';

export const SUPPLY_SITE_PAGE_CHANGE_REV_SHARE_MODAL = 'SUPPLY_SITE_PAGE_CHANGE_REV_SHARE_MODAL';
export const SUPPLY_SITE_PAGE_CHANGE_EXPENSES_MODAL = 'SUPPLY_SITE_PAGE_CHANGE_EXPENSES_MODAL';

export const SUPPLY_SITE_PAGE_TABLE_HEADERS = [
  {
    id: 'ID',
    label: 'Id',
    isSortable: true,
  },
  {
    id: 'NAME',
    label: 'Name',
    isSortable: true,
    defaultSortOrder: SORT_ASC,
  },
  {
    id: 'DEMANDS',
    label: 'Demand',
    isSortable: true,
  },
  // only for admin
  {
    id: 'PRICING',
    label: 'Rate',
    isSortable: true,
  },
  {
    id: 'SUPPLY_REQUESTS',
    label: 'Requests',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'IMPRESSIONS',
    label: 'Impressions',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  // only for admin
  {
    id: 'REVENUE',
    label: 'Revenue',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  // only for admin
  {
    id: 'GROSS_REVENUE',
    label: 'Gross Revenue',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  // only for self serve
  {
    id: 'PUB_REVENUE',
    label: 'Pub Revenue',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'PUB_NET_REVENUE',
    label: 'Pub NET Revenue',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'RPM',
    label: 'RPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  // only for admin
  {
    id: 'GROSS_RPM',
    label: 'Gross RPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'PUB_AD_RPM',
    label: 'Ad RPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'SUPPLY_REQUESTS_FILL',
    label: 'Req Fill',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  // only for admin
  {
    id: 'MEDIA_COST',
    label: 'Media Cost',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  // only for admin
  {
    id: 'CTR',
    label: 'CTR',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  // only for admin
  {
    id: 'COMPLETION_RATE',
    label: 'Completion Rate',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  {
    id: 'STATUS',
    label: 'Status',
  },
  // only for admin
  {
    id: 'CREATED',
    label: 'Creation Date',
    isSortable: true,
  },
  // only for admin
  {
    id: 'COPY_ACTION',
    label: '',
  },
];

export const SUPPLY_SITE_PAGE_TABLE_CELLS = [
  {
    key: 'id',
  },
  {
    key: 'name',
  },
  {
    key: 'demands',
  },
  {
    key: 'pricing',
    prefix: '$',
  },
  {
    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_REVENUE',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.PUB_NET_REVENUE',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.RPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.GROSS_RPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.PUB_AD_RPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.SUPPLY_REQUESTS_FILL',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  {
    key: 'fields.MEDIA_COST',
    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: 'status',
  },
  {
    key: 'created',
  },
  {
    key: 'copy',
  },
];

export const SUPPLY_SITE_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,
  },
  // only for admin
  {
    label: 'Revenue',
    value: 'REVENUE',
    prefix: '$',
  },
  // only for admin
  {
    label: 'Gross Revenue',
    value: 'GROSS_REVENUE',
    prefix: '$',
  },
  // only for self serve
  {
    label: 'Pub Revenue',
    value: 'PUB_REVENUE',
    prefix: '$',
  },
  {
    label: 'Pub NET Revenue',
    value: 'PUB_NET_REVENUE',
    prefix: '$',
  },
  // only for admin
  {
    label: 'Expenses',
    value: 'EXPENSES',
    prefix: '$',
  },
  // only for admin
  {
    label: 'Media Cost',
    value: 'MEDIA_COST',
    prefix: '$',
  },
  // only for admin
  {
    label: 'Media Margin',
    value: 'MEDIA_MARGIN',
    prefix: '$',
  },
  // only for admin
  {
    label: 'AC Ad RPM',
    value: 'AC_AD_RPM',
    prefix: '$',
  },
  // only for admin
  {
    label: 'Gross Ad RPM',
    value: 'GROSS_RPM',
    prefix: '$',
  },
  {
    label: 'Ad RPM',
    value: 'PUB_AD_RPM',
    prefix: '$',
  },
  // only for admin
  {
    label: 'CPM',
    value: 'CPM',
    prefix: '$',
  },
  {
    label: 'Player eRPM',
    value: 'PUB_PLAYER_ERPM',
    prefix: '$',
  },
  {
    label: 'Player eRPM',
    value: 'PLAYER_ERPM',
    prefix: '$',
  },
  // only for admin
  {
    label: 'Gross Player eRPM',
    value: 'GROSS_PLAYER_ERPM',
    prefix: '$',
  },
  {
    label: ' Pub NET Player eRPM',
    value: 'PUB_NET_PLAYER_ERPM',
    prefix: '$',
  },
  // only for admin
  {
    label: 'CTR',
    value: 'CTR',
    postfix: '%',
    needMultiply: true,
  },
  // only for admin
  {
    label: 'Completion Rate',
    value: 'COMPLETION_RATE',
    postfix: '%',
    needMultiply: true,
  },
  {
    label: 'Request Viewability',
    value: 'SUPPLY_REQUESTS_VIEWABILITY',
    postfix: '%',
    needMultiply: true,
  },
  {
    label: 'Impression Viewability',
    value: 'IMPRESSIONS_VIEWABILITY',
    postfix: '%',
    needMultiply: true,
  },
];

export const SUPPLY_SITE_PAGE_TOTAL_FIELDS = [
  'PLAYER_LOADS',
  'SUPPLY_REQUESTS',
  'IMPRESSIONS',
  'SUPPLY_REQUESTS_FILL',
  'OVERALL_FILL',
  'REVENUE',
  'PUB_REVENUE',
  'GROSS_REVENUE',
  'MEDIA_COST',
  'AC_AD_RPM',
  'GROSS_RPM',
  'PUB_AD_RPM',
  'CPM',
  'PUB_PLAYER_ERPM',
  'PLAYER_ERPM',
  'GROSS_PLAYER_ERPM',
  'SUPPLY_REQUESTS_VIEWABILITY',
  'IMPRESSIONS_VIEWABILITY',
  'MEDIA_MARGIN',
  'COMPLETION_RATE',
  'CTR',
  'EXPENSES',
  'PUB_NET_REVENUE',
  'PUB_NET_PLAYER_ERPM',
];

export const SUPPLY_SITE_PAGE_TOTAL_GRAPHQL_QUERY = `
  query SiteDataByIdQuery(
    $fields: [String],
    $filters: MarketplaceFiltersInputType,
    $id: String!
  ) {
    siteDataById(
      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
        }
        PUB_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
        }
        SUPPLY_REQUESTS_VIEWABILITY {
          value
          change
        }
        IMPRESSIONS_VIEWABILITY {
          value
          change
        }
        MEDIA_MARGIN {
          value
          change
        }
        COMPLETION_RATE {
          value
          change
        }
        CTR {
          value
          change
        }
        EXPENSES {
          value
          change
        }
        PUB_NET_REVENUE {
          value
          change
        }
        PUB_NET_PLAYER_ERPM {
          value
          change
        }
      }
    }
  }
`;

export const SUPPLY_SITE_PAGE_DATA_FIELDS = [
  'SUPPLY_REQUESTS',
  'IMPRESSIONS',
  'SUPPLY_REQUESTS_FILL',
  'REVENUE',
  'GROSS_REVENUE',
  'PUB_REVENUE',
  'RPM',
  'GROSS_RPM',
  'PUB_AD_RPM',
  'MEDIA_COST',
  'COMPLETION_RATE',
  'CTR',
  'PUB_NET_REVENUE',
];

export const SUPPLY_SITE_PAGE_DATA_GRAPHQL_QUERY = `
  query SupplyTagDataQuery(
    $fields: [String],
    $sort: MarketplaceSortInputType,
    $from: Int,
    $size: Int,
    $filters: MarketplaceFiltersInputType,
    $allPages: Boolean
  ) {
    supplyTagData(
      fields: $fields,
      sort: $sort,
      from: $from,
      size: $size,
      filters: $filters,
      allPages: $allPages
    ) {
      totalCount
      data {
        id
        name
        demands
        status
        accountId
        siteId
        created
        pricing {
          value
          model
        }
        fields {
          SUPPLY_REQUESTS {
            value
            change
          }
          IMPRESSIONS {
            value
            change
          }
          SUPPLY_REQUESTS_FILL {
            value
            change
          }
          REVENUE {
            value
            change
          }
          GROSS_REVENUE {
            value
            change
          }
          PUB_REVENUE {
            value
            change
          }
          RPM {
            value
            change
          }
          GROSS_RPM {
            value
            change
          }
          PUB_AD_RPM {
            value
            change
          }
          MEDIA_COST {
            value
            change
          }
          COMPLETION_RATE {
            value
            change
          }
          CTR {
            value
            change
          }
          PUB_NET_REVENUE {
            value
            change
          }
        }
      }
    }
  }
`;

export const SUPPLY_SITE_PAGE_HISTOGRAM_GRAPHQL_QUERY = `
  query SupplyTagHistogramQuery(
    $fields: [String],
    $interval: String,
    $timezone: String,
    $filters: [MarketplaceFiltersInputType]
  ) {
    supplyTagHistogram(
      fields: $fields,
      interval: $interval,
      timezone: $timezone,
      filters: $filters
    ) {
      totalCount
      data {
        name
        buckets {
          time
          fields {
            PAGE_VIEWS {
              value
              change
            }
            PLAYER_LOADS {
              value
              change
            }
            SUPPLY_REQUESTS {
              value
              change
            }
            IMPRESSIONS {
              value
              change
            }
            SUPPLY_REQUESTS_FILL {
              value
              change
            }
            OVERALL_FILL {
              value
              change
            }
            PUB_PLAYER_ERPM {
              value
              change
            }
            PLAYER_ERPM {
              value
              change
            }
            GROSS_PLAYER_ERPM {
              value
              change
            }
            REVENUE {
              value
              change
            }
            GROSS_REVENUE {
              value
              change
            }
            PUB_REVENUE {
              value
              change
            }
            MEDIA_COST {
              value
              change
            }
            RPM {
              value
              change
            }
            CPM {
              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_SITE_PAGE_INFO_GRAPHQL_QUERY = `
  query SiteByIdQuery(
    $id: String
  ) {
    siteById(
      id: $id
    ) {
      id
      name
      created
      updated
      updatedBy
      accountId
      accountName
      accountType
    }
  }
`;

export const SUPPLY_SITE_PAGE_CONFIG = {
  type: SUPPLY_SITE_PAGE,
  tabs: [
    {
      label: 'Supply Tags',
      dataId: 'main',
      contentKey: 'main',
    },
  ],
  nextLevelLink: SUPPLY_TAG_PAGE_PATHNAME,
  nextLevelQuery: 'tagName',
  infoRows: SUPPLY_SITE_PAGE_INFO_ROWS.filter(
    (item) => !['PUB_PLAYER_ERPM', 'PUB_REVENUE', 'PUB_AD_RPM'].includes(item.value),
  ),
  rowsInColumn: 12,
  tableSelecting: true,
  tableFilters: STATUS_FILTERS,
  tableHeaders: SUPPLY_SITE_PAGE_TABLE_HEADERS.filter(
    (item) => !['PUB_PLAYER_ERPM', 'PUB_REVENUE', 'PUB_AD_RPM'].includes(item.id),
  ),
  tableCells: SUPPLY_SITE_PAGE_TABLE_CELLS.filter(
    (item) => !['fields.PUB_PLAYER_ERPM', 'fields.PUB_REVENUE', 'fields.PUB_AD_RPM'].includes(item.key),
  ),
  timeIntervalOptions: COMMON_TIME_INTERVAL_OPTIONS,
  timeIntervalParams: COMMON_SUPPLY_TIME_INTERVAL_PARAMS,
  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: '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',
    },
  ],
  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_SITE_PAGE_TOTAL_FIELDS,
  totalQuery: SUPPLY_SITE_PAGE_TOTAL_GRAPHQL_QUERY,
  dataFields: SUPPLY_SITE_PAGE_DATA_FIELDS,
  dataQuery: SUPPLY_SITE_PAGE_DATA_GRAPHQL_QUERY,
  histogramQuery: SUPPLY_SITE_PAGE_HISTOGRAM_GRAPHQL_QUERY,
  infoQuery: SUPPLY_SITE_PAGE_INFO_GRAPHQL_QUERY,
  showDownloadCSVButton: true,
  showPlayerFilter: true,
  showCountriesFilter: true,
  showDevicesFilter: true,
  selfServeConfig: {
    infoRows: SUPPLY_SITE_PAGE_INFO_ROWS.filter(
      (item) =>
        ![
          'PLAYER_ERPM',
          'REVENUE',
          'GROSS_REVENUE',
          'EXPENSES',
          'MEDIA_COST',
          'MEDIA_MARGIN',
          'AC_AD_RPM',
          'CPM',
          'GROSS_PLAYER_ERPM',
          'CTR',
          'COMPLETION_RATE',
          'GROSS_RPM',
        ].includes(item.value),
    ),
    comparisonOptions: [
      {
        label: 'Player Loads',
        value: 'PLAYER_LOADS',
      },
      {
        label: 'Overall Fill ',
        value: 'OVERALL_FILL',
      },
      {
        label: 'Player eRPM',
        value: 'PUB_PLAYER_ERPM',
      },
      {
        label: 'Pub NET Player eRPM',
        value: 'PUB_NET_PLAYER_ERPM',
      },
      {
        label: 'Ad Requests',
        value: 'SUPPLY_REQUESTS',
      },
      {
        label: 'Ad Impressions',
        value: 'IMPRESSIONS',
      },
      {
        label: 'Request Fill',
        value: 'SUPPLY_REQUESTS_FILL',
      },
      {
        label: 'Pub Revenue',
        value: 'PUB_REVENUE',
      },
      {
        label: 'Pub NET Revenue',
        value: 'PUB_NET_REVENUE',
      },
      {
        label: 'Ad RPM',
        value: 'PUB_AD_RPM',
      },
      {
        label: 'Request Viewability',
        value: 'SUPPLY_REQUESTS_VIEWABILITY',
      },
      {
        label: 'Impression Viewability',
        value: 'IMPRESSIONS_VIEWABILITY',
      },
    ],
    tableHeaders: SUPPLY_SITE_PAGE_TABLE_HEADERS.filter(
      (item) =>
        ![
          'PLAYER_ERPM',
          'PRICING',
          'REVENUE',
          'GROSS_REVENUE',
          'GROSS_RPM',
          'MEDIA_COST',
          'CTR',
          'COMPLETION_RATE',
          'CREATED',
          'COPY_ACTION',
        ].includes(item.id),
    ),
    tableCells: SUPPLY_SITE_PAGE_TABLE_CELLS.filter(
      (item) =>
        ![
          'fields.PLAYER_ERPM',
          'pricing',
          'fields.REVENUE',
          'fields.GROSS_REVENUE',
          'fields.GROSS_RPM',
          'fields.MEDIA_COST',
          'fields.CTR',
          'fields.COMPLETION_RATE',
          'created',
          'copy',
        ].includes(item.key),
    ),
    // defaultConfig for Self Serve MP
    defaultConfig: {
      comparisonFilter: 'PLAYER_LOADS',
    },
  },
};
