import api from './axiosConfig'

// Auth API calls
export const authAPI = {
  login: (email, password) =>
    api.post('/admin/login', { email, password }),
  register: (data) =>
    api.post('/admin/register', data),
  refresh: (refreshToken) =>
    api.post('/admin/refresh', { refreshToken }),
  logout: () =>
    api.post('/admin/logout'),
}

// Users API calls
export const usersAPI = {
  getStats: () => api.get('/users/stats'),
  getUsers: (page = 1, limit = 10, search = '', startDate = '', endDate = '', filterType = 'all', subscriptionType = 'all') =>
    api.get('/users', {
      params: { page, limit, search, startDate, endDate, filterType, subscriptionType }
    }),
  exportUsers: (search, startDate, endDate, type = null, ids = null, subscriptionType = null) => {
    // If first argument is 'new_yesterday' or similar string, handle overload or just expect callers to pass nulls
    // Cleaner to use an object but sticking to positional for minimal refactor unless messy
    // Let's support object or positional to be safe if I can, but JS doesn't do that easily without checking.
    // Usage in Dashboard: exportUsers(type) -> exportUsers('new_yesterday')
    // Usage in Users: exportUsers(search, start, end)

    let params = {};
    if (typeof search === 'string' && !startDate && !endDate && !type && !ids && (search.startsWith('new_') || search === 'all')) {
      params.type = search;
    } else {
      if (search) params.search = search;
      if (startDate) params.startDate = startDate;
      if (endDate) params.endDate = endDate;
      if (type) params.type = type;
      if (ids) params.ids = ids.join(',');
      if (subscriptionType && subscriptionType !== 'all') params.subscriptionType = subscriptionType;
    }

    return api.get('/users/export', { responseType: 'blob', params });
  },
  getUser: (id) =>
    api.get(`/users/${id}`),
  createUser: (data) =>
    api.post('/users', data),
  updateUser: (id, data) =>
    api.put(`/users/${id}`, data),
  deleteUser: (id) =>
    api.delete(`/users/${id}`),
  softDeleteUser: (id) =>
    api.delete(`/users/${id}`),
  restoreUser: (id) =>
    api.put(`/users/${id}/restore`),
}

// Activity Logs API
export const activityLogsAPI = {
  getLogs: (page = 1, limit = 20, search = '', logType = '') =>
    api.get('/activity-logs', { params: { page, limit, search, logType } }),
  getDashboardLogs: (limit = 20) =>
    api.get('/activity-logs/dashboard', { params: { limit } }),
  getTypes: () => api.get('/activity-logs/types')
}

// Notifications API
export const notificationsAPI = {
  getAll: (page = 1, limit = 10, search = '', startDate = '', endDate = '', filterType = 'all') =>
    api.get('/notifications', { params: { page, limit, search, startDate, endDate, filterType } }),
  getById: (id) => api.get(`/notifications/${id}`),
  create: (data) => api.post('/notifications', data),
  update: (id, data) => api.put(`/notifications/${id}`, data),
  delete: (id) => api.delete(`/notifications/${id}`),
  restore: (id) => api.put(`/notifications/${id}/restore`),
}
