import axios from 'src/utils/axios';

// ----------------------------------------------------------------------

/**
 * Последние 30 дней (диапазон по умолчанию)
 */
export function getLast30DaysRange() {
  const now = new Date();
  const end = new Date(now);
  const start = new Date(now);
  start.setDate(start.getDate() - 29);
  const fmt = (d) => d.toISOString().slice(0, 10);
  return { start_date: fmt(start), end_date: fmt(end) };
}

function buildParams(range) {
  const p = new URLSearchParams();
  p.set('period', range.period || 'custom');
  p.set('start_date', range.start_date);
  p.set('end_date', range.end_date);
  return p.toString();
}

/**
 * GET /analytics/overview?period=custom&start_date=&end_date=
 */
export async function getAnalyticsOverview(range) {
  const q = buildParams(range);
  const res = await axios.get(`/analytics/overview?${q}`);
  return res.data;
}

/**
 * GET /analytics/students?...
 */
export async function getAnalyticsStudents(range) {
  const q = buildParams(range);
  const res = await axios.get(`/analytics/students?${q}`);
  return res.data;
}

/**
 * GET /analytics/revenue?...
 */
export async function getAnalyticsRevenue(range) {
  const q = buildParams(range);
  const res = await axios.get(`/analytics/revenue?${q}`);
  return res.data;
}

/**
 * GET /analytics/grades_by_day?...
 */
export async function getAnalyticsGradesByDay(range) {
  const q = buildParams(range);
  const res = await axios.get(`/analytics/grades_by_day?${q}`);
  return res.data;
}
