const TOKEN_KEY = 'fitcrm_access_token';
const REFRESH_TOKEN_KEY = 'fitcrm_refresh_token';

const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000/v1';

export interface AuthTokens {
  accessToken: string;
  refreshToken: string;
}

export async function login(
  phone: string,
  password: string,
): Promise<AuthTokens> {
  const response = await fetch(`${API_URL}/auth/login`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ phone, password }),
  });

  if (!response.ok) {
    const error: Record<string, unknown> = await (response.json() as Promise<Record<string, unknown>>).catch(() => ({}));
    throw new Error(typeof error.message === 'string' ? error.message : 'Ошибка авторизации');
  }

  const data = (await response.json()) as AuthTokens;

  localStorage.setItem(TOKEN_KEY, data.accessToken);
  localStorage.setItem(REFRESH_TOKEN_KEY, data.refreshToken);

  return data;
}

export async function refreshToken(): Promise<string> {
  const refresh = localStorage.getItem(REFRESH_TOKEN_KEY);

  if (!refresh) {
    throw new Error('Нет refresh-токена');
  }

  const response = await fetch(`${API_URL}/auth/refresh`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ refreshToken: refresh }),
  });

  if (!response.ok) {
    localStorage.removeItem(TOKEN_KEY);
    localStorage.removeItem(REFRESH_TOKEN_KEY);
    throw new Error('Сессия истекла');
  }

  const data = (await response.json()) as AuthTokens;
  localStorage.setItem(TOKEN_KEY, data.accessToken);

  if (data.refreshToken) {
    localStorage.setItem(REFRESH_TOKEN_KEY, data.refreshToken);
  }

  return data.accessToken;
}

export function logout(): void {
  localStorage.removeItem(TOKEN_KEY);
  localStorage.removeItem(REFRESH_TOKEN_KEY);
  window.location.href = '/login';
}

export function getAccessToken(): string | null {
  if (typeof window === 'undefined') return null;
  return localStorage.getItem(TOKEN_KEY);
}

export function getAuthHeaders(): Record<string, string> {
  const token = getAccessToken();
  if (!token) return {};
  return { Authorization: `Bearer ${token}` };
}

export function isAuthenticated(): boolean {
  return !!getAccessToken();
}
