import axios from 'axios';

const api = axios.create({ baseURL: '/api' });

export const gsc = {
  sites:    ()                           => api.get('/gsc/sites').then(r => r.data),
  metrics:  (site, days = 28)            => api.get('/gsc/metrics',   { params: { site, days } }).then(r => r.data),
  keywords: (site, days = 28, limit = 50)=> api.get('/gsc/keywords',  { params: { site, days, limit } }).then(r => r.data),
  pages:    (site, days = 28)            => api.get('/gsc/pages',     { params: { site, days } }).then(r => r.data),
  history:   (site, days = 90)            => api.get('/gsc/history',   { params: { site, days } }).then(r => r.data),
  kpiPages:  (site, pages, days = 28)     => api.post('/gsc/kpi-pages', { site, pages, days }).then(r => r.data),
};

export const ahrefs = {
  overview:    (domain)                         => api.get('/ahrefs/overview',    { params: { domain } }).then(r => r.data),
  keywords:    (domain, limit = 50, country='us')=> api.get('/ahrefs/keywords',   { params: { domain, limit, country } }).then(r => r.data),
  history:     (domain, days = 90)              => api.get('/ahrefs/history',    { params: { domain, days } }).then(r => r.data),
  pages:       (domain, limit = 20)             => api.get('/ahrefs/pages',      { params: { domain, limit } }).then(r => r.data),
  backlinks:   (domain, limit = 20)             => api.get('/ahrefs/backlinks',  { params: { domain, limit } }).then(r => r.data),
  competitors: (domain, limit = 10)             => api.get('/ahrefs/competitors',{ params: { domain, limit } }).then(r => r.data),
  refdomains:  (domain, limit = 20)             => api.get('/ahrefs/refdomains', { params: { domain, limit } }).then(r => r.data),
};

export const clickup = {
  spaces:       ()           => api.get('/clickup/spaces').then(r => r.data),
  folders:      ()           => api.get('/clickup/folders').then(r => r.data),
  tasks:        (folderIds)  => api.get('/clickup/tasks', { params: { folderIds: Array.isArray(folderIds) ? folderIds.join(',') : folderIds || '' } }).then(r => r.data),
  summary:      ()           => api.get('/clickup/summary').then(r => r.data),
  updateStatus:  (id, status)               => api.post(`/clickup/tasks/${id}/status`, { status }).then(r => r.data),
  members:       ()                          => api.get('/clickup/members').then(r => r.data),
  teamOverview:  (folderIds)                 => api.get('/clickup/team-overview', { params: { folderIds: Array.isArray(folderIds) ? folderIds.join(',') : folderIds || '' } }).then(r => r.data),
};

export const insights = {
  site:    (payload) => api.post('/insights/site',    payload).then(r => r.data),
  keyword: (payload) => api.post('/insights/keyword', payload).then(r => r.data),
};

export const sheets = {
  list: ()          => api.get('/sheets/list').then(r => r.data),
  all:  ()          => api.get('/sheets/all').then(r => r.data),
  data: (id, tab)   => api.get('/sheets/data', { params: { id, tab } }).then(r => r.data),
};
