import { createContext, useContext, useState, useCallback, useEffect, type ReactNode } from 'react'
import { api, endpoints, getApiBase, setToken } from '../api'
import type { AuthUser } from '../types'

interface AuthState {
  user: AuthUser | null
  loading: boolean
  login: (email: string, password: string) => Promise<void>
  logout: () => Promise<void>
  hasPermission: (perm: string) => boolean
}

const AuthContext = createContext<AuthState>(null!)

export function useAuth() {
  return useContext(AuthContext)
}

// React 18+ StrictMode double-invokes effects in dev (mount → cleanup → mount),
// and the backend rotates the refresh cookie on every successful POST. Two
// concurrent bootstraps would race the rotation and leave the second one with
// a stale cookie. The module-scoped in-flight promise lets the second mount
// await the first's network round-trip instead of duplicating it. Risk 4 in
// AZ-510 spec.
let bootstrapInflight: Promise<AuthUser | null> | null = null

/**
 * Test-only hook to clear the module-scoped in-flight bootstrap promise
 * between Vitest tests. Production never imports this — it exists because
 * Vitest does not reset module state between tests, so a test that mocks the
 * bootstrap to never-resolve would otherwise leak a permanently-pending
 * promise that subsequent tests would await forever. Wired into
 * `tests/setup.ts` afterEach. Safe-no-op when nothing is in flight.
 */
export function __resetBootstrapInflightForTests(): void {
  bootstrapInflight = null
}

async function runBootstrap(): Promise<AuthUser | null> {
  // POST refresh with credentials — the whole point of the consolidation. Goes
  // through fetch() directly (not api.post) because api.post does not thread
  // credentials:'include'; widening api.post would change CORS posture for
  // every authenticated callsite. Same pattern lives in api/client.ts:88 for
  // the 401-retry refresh path.
  const refreshRes = await fetch(getApiBase() + endpoints.admin.authRefresh(), {
    method: 'POST',
    credentials: 'include',
  })
  if (!refreshRes.ok) return null
  const refreshData = (await refreshRes.json()) as { token: string }
  setToken(refreshData.token)
  try {
    return await api.get<AuthUser>(endpoints.admin.usersMe())
  } catch (err) {
    // Refresh succeeded but /users/me failed — clear the bearer so an in-flight
    // re-render does not see (user: null) alongside an active accessToken
    // (Constraint #4 in spec).
    console.error('[AuthContext] Refresh succeeded but /users/me failed:', err)
    setToken(null)
    return null
  }
}

export function AuthProvider({ children }: { children: ReactNode }) {
  const [user, setUser] = useState<AuthUser | null>(null)
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    let cancelled = false
    const inflight =
      bootstrapInflight ??
      (bootstrapInflight = runBootstrap().finally(() => {
        bootstrapInflight = null
      }))
    inflight
      .then(result => {
        if (cancelled) return
        setUser(result)
        setLoading(false)
      })
      .catch(err => {
        // Network error on the POST refresh itself (the /users/me failure path
        // is handled inside runBootstrap and resolves to null). Reliability NFR
        // requires loading to flip to false on every failure path.
        console.error('[AuthContext] Bootstrap failed:', err)
        if (cancelled) return
        setToken(null)
        setUser(null)
        setLoading(false)
      })
    return () => {
      cancelled = true
    }
  }, [])

  const login = useCallback(async (email: string, password: string) => {
    const data = await api.post<{ token: string; user: AuthUser }>(endpoints.admin.authLogin(), { email, password })
    setToken(data.token)
    setUser(data.user)
  }, [])

  const logout = useCallback(async () => {
    try { await api.post(endpoints.admin.authLogout()) } catch {}
    setToken(null)
    setUser(null)
  }, [])

  const hasPermission = useCallback((perm: string) => {
    // `permissions` is required by the AuthUser type but the runtime payload
    // from `/users/me` may omit it (older backend builds, or test fixtures
    // returning the bare User shape). Treat missing as "no permissions" rather
    // than crashing the React tree.
    return user?.permissions?.includes(perm) ?? false
  }, [user])

  return (
    <AuthContext.Provider value={{ user, loading, login, logout, hasPermission }}>
      {children}
    </AuthContext.Provider>
  )
}
