const axios = require('axios');

const NBA_SERVICE_URL = process.env.NBA_SERVICE_URL || 'http://localhost:8000';
const TIMEOUT = 10000;

async function getSeasonAvg(player, statType, season) {
  const params = { player };
  if (statType) params.stat_type = statType;
  if (season) params.season = season;

  const { data } = await axios.get(`${NBA_SERVICE_URL}/stats/season-avg`, {
    params,
    timeout: TIMEOUT,
  });
  return data;
}

async function getLastN(player, n = 10, statType) {
  const params = { player, n };
  if (statType) params.stat_type = statType;

  const { data } = await axios.get(`${NBA_SERVICE_URL}/stats/last-n`, {
    params,
    timeout: TIMEOUT,
  });
  return data;
}

async function getSplits(player, statType, splitType, opponent) {
  const params = { player, stat_type: statType, split_type: splitType };
  if (opponent) params.opponent = opponent;

  const { data } = await axios.get(`${NBA_SERVICE_URL}/stats/splits`, {
    params,
    timeout: TIMEOUT,
  });
  return data;
}

async function searchPlayer(name) {
  const { data } = await axios.get(`${NBA_SERVICE_URL}/players/search`, {
    params: { name },
    timeout: TIMEOUT,
  });
  return data;
}

module.exports = { getSeasonAvg, getLastN, getSplits, searchPlayer };
