import { ref } from 'vue'

export const isParentAuthenticated = ref(false)
export const isUserLoggedIn = ref(false)
export const isAuthReady = ref(false)
export const currentUserId = ref('')

export function authenticateParent() {
  isParentAuthenticated.value = true
}

export function logoutParent() {
  isParentAuthenticated.value = false
}

export function loginUser() {
  isUserLoggedIn.value = true
}

export function logoutUser() {
  isUserLoggedIn.value = false
}

export async function checkAuth() {
  try {
    const res = await fetch('/api/me', { method: 'GET' })
    if (res.ok) {
      const data = await res.json()
      currentUserId.value = data.id
      isUserLoggedIn.value = true
    } else {
      isUserLoggedIn.value = false
      currentUserId.value = ''
    }
  } catch {
    isUserLoggedIn.value = false
    currentUserId.value = ''
  }
  isAuthReady.value = true
}
