export interface AdminUser {
  id: string;
  email: string;
  name: string | null;
  role: string;
  permissions: string[];
}

export async function checkAdminAuth(): Promise<AdminUser | null> {
  try {
    const response = await fetch('/api/admin/auth/me', {
      credentials: 'include'
    });

    if (response.ok) {
      const data = await response.json();
      return data.user;
    }

    return null;
  } catch (error) {
    console.error('Admin auth check error:', error);
    return null;
  }
}

export async function adminLogin(email: string, password: string): Promise<{ success: boolean; user?: AdminUser; error?: string }> {
  try {
    const response = await fetch('/api/admin/auth/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      credentials: 'include',
      body: JSON.stringify({ email, password }),
    });

    const data = await response.json();

    if (response.ok) {
      return { success: true, user: data.user };
    } else {
      return { success: false, error: data.error };
    }
  } catch (error) {
    return { success: false, error: 'Network error' };
  }
}

export async function adminLogout(): Promise<boolean> {
  try {
    const response = await fetch('/api/admin/auth/logout', {
      method: 'POST',
      credentials: 'include'
    });

    return response.ok;
  } catch (error) {
    console.error('Admin logout error:', error);
    return false;
  }
}