import {
  currencySymbols,
  DATA_URL,
  PROCEDURES,
} from 'config'
import { callApi } from 'helpers'

const proc = PROCEDURES.get_match_subscriptions

export type Subscription = {
  id: number,
  lexic: number,
  lexic2: number,
  lexic3: number,
  min_price?: number,
  packages: PackagesGroup,
}

export type Subscriptions = Array<Subscription>

export type SubscriptionsData = {
  data?: Subscriptions,
  season: Season,
}

type PackagesGroup = {
  month: Packages,
  pay_per_view: SubscriptionResponse,
  year: Packages,
}

export type Packages = {
  all: SubscriptionResponse,
  team1: SubscriptionResponse,
  team2: SubscriptionResponse,
  team_away: SubscriptionResponse,
  team_home: SubscriptionResponse,
}

export type SubscriptionResponse = {
  currency_id: number,
  currency_iso: keyof typeof currencySymbols,
  id: number,
  price: number,
  sub: Sub,
}

type Sub = {
  active_from: string,
  active_to: string,
  currency_id: number,
  id: number,
  lexic1: number,
  lexic2: number | null,
  lexic3: number,
  match_id?: number,
  option: number,
  price: number,
  sport?: number,
  team_id?: number,
}

export type Season = {
  id: number,
  name: string,
}

export const getSubscriptions = async (
  sport: number,
  matchId: number,
): Promise<SubscriptionsData> => {
  const config = {
    body: {
      params: {
        _p_match_id: matchId,
        _p_sport: sport,
      },
      proc,
    },
  }

  return callApi({
    config,
    url: DATA_URL,
  })
}
