import { useState, useRuntimeConfig } from '#imports'
import type { Profile } from './useProfiles'

const buildUrl = (path: string): string => {
  const config = useRuntimeConfig()
  const base = ((config.public.apiBaseUrl as string) || '').replace(/\/$/, '')
  return `${base}${path}`
}

export function useProfileSession() {
  const activeProfile = useState<Profile | null>('profileSession:active', () => null)
  const sessionLoaded = useState<boolean>('profileSession:loaded', () => false)
  const loading = useState<boolean>('profileSession:loading', () => false)

  const fetchCurrentProfile = async (): Promise<Profile | null> => {
    loading.value = true
    try {
      activeProfile.value = await $fetch<Profile>(buildUrl('/session/profile'), {
        method: 'GET',
        credentials: 'include',
      })
    } catch (error) {
      const err = error as { status?: number }
      if (err?.status === 401) {
        activeProfile.value = null
      } else {
        console.error('Erreur lors du chargement du profil actif', error)
        activeProfile.value = null
      }
    } finally {
      sessionLoaded.value = true
      loading.value = false
    }
    return activeProfile.value
  }

  const ensureSession = (): Promise<Profile | null> => {
    if (!sessionLoaded.value) {
      return fetchCurrentProfile()
    }
    return Promise.resolve(activeProfile.value)
  }

  const activateProfile = async (profileId: string, password?: string): Promise<void> => {
    const body: Record<string, string> = { profileId }
    if (password) {
      body.password = password
    }
    await $fetch(buildUrl('/session/profile'), {
      method: 'POST',
      credentials: 'include',
      body,
    })
    await fetchCurrentProfile()
  }

  const logout = async (): Promise<void> => {
    try {
      await $fetch(buildUrl('/session/profile'), {
        method: 'DELETE',
        credentials: 'include',
      })
    } finally {
      activeProfile.value = null
      sessionLoaded.value = true
    }
  }

  return {
    activeProfile,
    loading,
    sessionLoaded,
    ensureSession,
    fetchCurrentProfile,
    activateProfile,
    logout,
  }
}
