const API_URL = process.env.NEXT_PUBLIC_API_URL || 'https://wellnuo.smartlaunchhub.com';

export async function apiRequest(endpoint, options = {}) {
  const token = typeof window !== 'undefined' ? localStorage.getItem('adminToken') : null;

  const headers = {
    'Content-Type': 'application/json',
    ...options.headers,
  };

  if (token) {
    headers['Authorization'] = `Bearer ${token}`;
  }

  const res = await fetch(`${API_URL}${endpoint}`, {
    ...options,
    headers,
  });

  if (res.status === 401) {
    if (typeof window !== 'undefined') {
      localStorage.removeItem('adminToken');
      window.location.href = '/admin/login';
    }
    throw new Error('Unauthorized');
  }

  const data = await res.json();

  if (!res.ok) {
    throw new Error(data.error || 'Request failed');
  }

  return data;
}

// Auth
export const requestOTP = (email) =>
  apiRequest('/api/auth/request-otp', {
    method: 'POST',
    body: JSON.stringify({ email }),
  });

export const verifyOTP = (email, code) =>
  apiRequest('/api/auth/verify-otp', {
    method: 'POST',
    body: JSON.stringify({ email, code }),
  });

export const getMe = () => apiRequest('/api/auth/me');

// Admin endpoints
export const getStats = () => apiRequest('/api/admin/stats');
export const getOrders = (status) => apiRequest(`/api/admin/orders${status ? `?status=${status}` : ''}`);
export const getOrder = (id) => apiRequest(`/api/admin/orders/${id}`);
export const updateOrder = (id, data) =>
  apiRequest(`/api/admin/orders/${id}`, {
    method: 'PATCH',
    body: JSON.stringify(data),
  });

export const getUsers = () => apiRequest('/api/admin/users');
export const getUser = (id) => apiRequest(`/api/admin/users/${id}`);

export const getBeneficiaries = () => apiRequest('/api/admin/beneficiaries');
export const getSubscriptions = () => apiRequest('/api/admin/subscriptions');

// Deployments
export const getDeployments = () => apiRequest('/api/admin/deployments');
export const getDeployment = (id) => apiRequest(`/api/admin/deployments/${id}`);

// Devices
export const getDevices = (wellId) => apiRequest(`/api/admin/devices${wellId ? `?well_id=${wellId}` : ''}`);
export const getDevice = (id) => apiRequest(`/api/admin/devices/${id}`);
