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

export const DEMAND_ACCOUNTS_PAGE_PATHNAME = '/demand';

export const DEMAND_ACCOUNTS_PAGE_TYPE = 'DEMAND_ACCOUNTS_PAGE';

export const DEMAND_ACCOUNTS_PAGE_HEADER = 'Demand Accounts';

export const DEMAND_ACCOUNTS_PAGE_HEADERS = [
  {
    id: 'NAME',
    label: 'Name',
    isSortable: true,
    defaultSortOrder: SORT_ASC,
  },
  {
    id: 'ADVERTISERS',
    label: 'Advertisers',
    isSortable: true,
  },
  {
    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,
  },
];

export const DEMAND_ACCOUNTS_PAGE_CELLS = [
  {
    key: 'name',
  },
  {
    key: 'advertisers',
  },
  {
    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,
  },
];

export const DEMAND_ACCOUNTS_PAGE_FIELDS = [
  'REQUESTS',
  'IMPRESSIONS',
  'REQUESTS_FILL',
  'REVENUE',
  'GROSS_REVENUE',
  'RPM',
  'GROSS_RPM',
];

export const DEMAND_ACCOUNTS_PAGE_GRAPHQL_QUERY = `
  query DemandAccountsDataQuery(
    $fields: [String],
    $sort: MarketplaceSortInputType,
    $from: Int,
    $size: Int,
    $filters: MarketplaceFiltersInputType,
    $allPages: Boolean
  ) {
    demandAccountsData(
      fields: $fields,
      sort: $sort,
      from: $from,
      size: $size,
      filters: $filters,
      allPages: $allPages
    ) {
      totalCount
      data {
        id
        name
        advertisers
        demands
        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
          }
        }
      }
    }
  }
`;

export const DEMAND_ACCOUNTS_PAGE_ADVERTISERS_HEADERS = [
  {
    id: 'NAME',
    label: 'Name',
    isSortable: true,
  },
  {
    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: 'PROFIT',
    label: 'Profit',
    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: 'IMPRESSIONS_VIEWABILITY',
    label: 'Impressions viewability',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  // only for admin
  {
    id: 'REQ_ERPM',
    label: 'Req eRPM',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  // only for admin
  {
    id: 'REQ_EPPM',
    label: 'Req ePPM',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  // only for admin
  {
    id: 'REQ_NET_ERPM',
    label: 'Req Pub NET eRPM',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  // only for self serve
  {
    id: 'REQ_GROSS_ERPM',
    label: 'Req Gross eRPM ',
    align: 'right',
    withGap: true,
    isSortable: 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 DEMAND_ACCOUNTS_PAGE_ADVERTISERS_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.PROFIT',
    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.IMPRESSIONS_VIEWABILITY',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  // only for admin
  {
    key: 'fields.REQ_ERPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  // only for admin
  {
    key: 'fields.REQ_EPPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  // only for admin
  {
    key: 'fields.REQ_NET_ERPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  // only for self serve
  {
    key: 'fields.REQ_GROSS_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 DEMAND_ACCOUNTS_PAGE_ADVERTISERS_FIELDS = [
  'REQUESTS',
  'IMPRESSIONS',
  'REQUESTS_FILL',
  'REVENUE',
  'GROSS_REVENUE',
  'RPM',
  'GROSS_RPM',
  'ERPM',
  'IMPRESSIONS_VIEWABILITY',
  'COMPLETION_RATE',
  'CTR',
  'REQ_ERPM',
  'REQ_EPPM',
  'REQ_NET_ERPM',
  'REQ_GROSS_ERPM',
  'PROFIT',
];

export const DEMAND_ACCOUNTS_PAGE_ADVERTISERS_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
      daccountId
      profit
      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
        }
        IMPRESSIONS_VIEWABILITY {
          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_ACCOUNTS_PAGE_TAGS_HEADERS = [
  {
    id: 'NAME',
    label: 'Name',
    isSortable: true,
  },
  {
    id: 'SUPPLIES',
    label: 'Supply Tags',
    isSortable: true,
  },
  {
    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: 'RPM',
    label: 'RPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'GROSS_RPM',
    label: 'Gross RPM',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  {
    id: 'IMPRESSIONS_VIEWABILITY',
    label: 'Impressions viewability',
    isSortable: true,
    align: 'right',
    withGap: true,
  },
  // only for admin
  {
    id: 'REQ_ERPM',
    label: 'Req eRPM',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  // only for admin
  {
    id: 'REQ_EPPM',
    label: 'Req ePPM',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  // only for admin
  {
    id: 'REQ_NET_ERPM',
    label: 'Req Pub NET eRPM',
    align: 'right',
    withGap: true,
    isSortable: true,
  },
  // only for self serve
  {
    id: 'REQ_GROSS_ERPM',
    label: 'Req Gross eRPM ',
    align: 'right',
    withGap: true,
    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,
  },
];

export const DEMAND_ACCOUNTS_PAGE_TAGS_CELLS = [
  {
    key: 'name',
  },
  {
    key: 'supplies',
  },
  {
    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.RPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.GROSS_RPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  {
    key: 'fields.IMPRESSIONS_VIEWABILITY',
    align: 'right',
    postfix: '%',
    needMultiply: true,
    withPercent: true,
  },
  // only for admin
  {
    key: 'fields.REQ_ERPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  // only for admin
  {
    key: 'fields.REQ_EPPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  // only for admin
  {
    key: 'fields.REQ_NET_ERPM',
    align: 'right',
    prefix: '$',
    withPercent: true,
  },
  // only for self serve
  {
    key: 'fields.REQ_GROSS_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,
  },
  {
    key: 'created',
  },
];

export const DEMAND_ACCOUNTS_PAGE_TAGS_FIELDS = [
  'REQUESTS',
  'OPPORTUNITIES',
  'IMPRESSIONS',
  'REQUESTS_FILL',
  'REVENUE',
  'GROSS_REVENUE',
  'RPM',
  'GROSS_RPM',
  'ERPM',
  'IMPRESSIONS_VIEWABILITY',
  'COMPLETION_RATE',
  'CTR',
  'REQ_ERPM',
  'REQ_EPPM',
  'REQ_NET_ERPM',
  'REQ_GROSS_ERPM',
  'PROFIT',
];

export const DEMAND_ACCOUNTS_PAGE_TAGS_GRAPHQL_QUERY = `
query DemandTagDataQuery(
  $fields: [String],
  $sort: MarketplaceSortInputType,
  $from: Int,
  $size: Int,
  $filters: MarketplaceFiltersInputType,
  $allPages: Boolean
) {
  demandTagData(
    fields: $fields,
    sort: $sort,
    from: $from,
    size: $size,
    filters: $filters,
    allPages: $allPages
  ) {
    totalCount
    data {
      id
      name
      supplies
      aps
      advertiserId
      daccountId
      created
      waterfallSkip
      viewabilityThreshold
      profit
      include {
        domains
        geo
        os
        browsers
        playerSizes
        viewability
        devices
      }
      exclude {
        domains
        geo
        os
        browsers
        devices
      }
      frequency {
        status
        value
        amount
        type
        timeframe
      }
      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
        }
        REVENUE {
          value
          change
        }
        GROSS_REVENUE {
          value
          change
        }
        RPM {
          value
          change
        }
        GROSS_RPM {
          value
          change
        }
        IMPRESSIONS_VIEWABILITY {
          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_ACCOUNTS_PAGE_CONFIG = {
  type: DEMAND_ACCOUNTS_PAGE_TYPE,
  pathname: DEMAND_ACCOUNTS_PAGE_PATHNAME,
  header: DEMAND_ACCOUNTS_PAGE_HEADER,
  rowsPerPageOptions: [10, 25, 50, 100, 200],
  tabs: [
    {
      label: 'Demand Accounts',
      dataId: 'accounts',
      tableHeaders: DEMAND_ACCOUNTS_PAGE_HEADERS,
      tableCells: DEMAND_ACCOUNTS_PAGE_CELLS,
      graphQuery: DEMAND_ACCOUNTS_PAGE_GRAPHQL_QUERY,
      fields: DEMAND_ACCOUNTS_PAGE_FIELDS,
      createRowLink: ({ pathname, id, name }) => `${pathname}/${id}?accountName=${encodeURIComponent(name)}`,
    },
    {
      label: 'Advertisers',
      dataId: 'advertisers',
      tableHeaders: DEMAND_ACCOUNTS_PAGE_ADVERTISERS_HEADERS.filter((item) => !['REQ_GROSS_ERPM'].includes(item.id)),
      tableCells: DEMAND_ACCOUNTS_PAGE_ADVERTISERS_CELLS.filter(
        (item) => !['fields.REQ_GROSS_ERPM'].includes(item.key),
      ),
      graphQuery: DEMAND_ACCOUNTS_PAGE_ADVERTISERS_GRAPHQL_QUERY,
      fields: DEMAND_ACCOUNTS_PAGE_ADVERTISERS_FIELDS,
      createRowLink: ({ pathname, id, name, daccountId }) =>
        `${pathname}/${daccountId}/advertisers/${id}?advertiserName=${encodeURIComponent(name)}`,
      tabPlusButton: {
        label: 'Advertiser',
        onClick: (router) => {
          router.push({
            pathname: `${router.pathname}/new/advertisers/new`,
          });
        },
      },
    },
    {
      label: 'Demand Tags',
      dataId: 'tags',
      tableHeaders: DEMAND_ACCOUNTS_PAGE_TAGS_HEADERS.filter((item) => !['REQ_GROSS_ERPM'].includes(item.id)),
      tableCells: DEMAND_ACCOUNTS_PAGE_TAGS_CELLS.filter((item) => !['fields.REQ_GROSS_ERPM'].includes(item.key)),
      graphQuery: DEMAND_ACCOUNTS_PAGE_TAGS_GRAPHQL_QUERY,
      fields: DEMAND_ACCOUNTS_PAGE_TAGS_FIELDS,
      createRowLink: ({ pathname, id, name, daccountId, advertiserId }) =>
        `${pathname}/${daccountId}/advertisers/${advertiserId}/tags/${id}?tagName=${encodeURIComponent(name)}`,
    },
  ],
  selfServeTabs: [
    {
      label: 'Advertisers',
      dataId: 'advertisers',
      tableHeaders: DEMAND_ACCOUNTS_PAGE_ADVERTISERS_HEADERS.filter(
        (item) => !['REVENUE', 'RPM', 'PROFIT', '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_RPM') {
          return { ...item, label: 'Ad RPM' };
        }
        if (item.id === 'REQ_GROSS_ERPM') {
          return { ...item, label: 'Req eRPM' };
        }
        return item;
      }),
      tableCells: DEMAND_ACCOUNTS_PAGE_ADVERTISERS_CELLS.filter(
        (item) =>
          ![
            'fields.REVENUE',
            'fields.RPM',
            'fields.PROFIT',
            'fields.REQ_ERPM',
            'fields.REQ_EPPM',
            'fields.REQ_NET_ERPM',
          ].includes(item.key),
      ),
      graphQuery: DEMAND_ACCOUNTS_PAGE_ADVERTISERS_GRAPHQL_QUERY,
      fields: DEMAND_ACCOUNTS_PAGE_ADVERTISERS_FIELDS,
      createRowLink: ({ pathname, id, name, daccountId }) =>
        `${pathname}/${daccountId}/advertisers/${id}?advertiserName=${encodeURIComponent(name)}`,
      tabPlusButton: {
        label: 'Advertiser',
        onClick: (router) => {
          router.push({
            pathname: `${router.pathname}/new/advertisers/new`,
          });
        },
      },
    },
    {
      label: 'Demand Tags',
      dataId: 'tags',
      tableHeaders: DEMAND_ACCOUNTS_PAGE_TAGS_HEADERS.filter(
        (item) => !['REVENUE', 'RPM', 'PROFIT', '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_RPM') {
          return { ...item, label: 'Ad RPM' };
        }
        if (item.id === 'REQ_GROSS_ERPM') {
          return { ...item, label: 'Req eRPM' };
        }
        return item;
      }),
      tableCells: DEMAND_ACCOUNTS_PAGE_TAGS_CELLS.filter(
        (item) =>
          ![
            'fields.REVENUE',
            'fields.RPM',
            'fields.PROFIT',
            'fields.REQ_ERPM',
            'fields.REQ_EPPM',
            'fields.REQ_NET_ERPM',
          ].includes(item.key),
      ),
      graphQuery: DEMAND_ACCOUNTS_PAGE_TAGS_GRAPHQL_QUERY,
      fields: DEMAND_ACCOUNTS_PAGE_TAGS_FIELDS,
      createRowLink: ({ pathname, id, name, daccountId, advertiserId }) =>
        `${pathname}/${daccountId}/advertisers/${advertiserId}/tags/${id}?tagName=${encodeURIComponent(name)}`,
    },
  ],
};
