"use client"

import {
	createContext,
	type ReactNode,
	useCallback,
	useContext,
	useEffect,
	useState,
} from "react"
import { authClient, useSession } from "./auth"

type Organization = typeof authClient.$Infer.ActiveOrganization
type SessionData = NonNullable<ReturnType<typeof useSession>["data"]>

interface AuthContextType {
	session: SessionData["session"] | null
	user: SessionData["user"] | null
	org: Organization | null
	setActiveOrg: (orgSlug: string) => Promise<void>
	updateOrgMetadata: (partial: Record<string, unknown>) => void
}

const AuthContext = createContext<AuthContextType | undefined>(undefined)

export function AuthProvider({ children }: { children: ReactNode }) {
	const { data: session } = useSession()
	const [org, setOrg] = useState<Organization | null>(null)
	const { data: orgs } = authClient.useListOrganizations()

	const setActiveOrg = async (slug: string) => {
		if (!slug) return

		const activeOrg = await authClient.organization.setActive({
			organizationSlug: slug,
		})
		setOrg(activeOrg)
		localStorage.setItem("supermemory-consumer-last-org-slug", slug)
	}

	const updateOrgMetadata = useCallback((partial: Record<string, unknown>) => {
		setOrg((prev) => {
			if (!prev) return prev
			return {
				...prev,
				metadata: {
					...prev.metadata,
					...partial,
				},
			}
		})
	}, [])

	// biome-ignore lint/correctness/useExhaustiveDependencies: ignoring the setActiveOrg dependency
	useEffect(() => {
		if (!session?.session.activeOrganizationId || !orgs) return

		const savedSlug = localStorage.getItem("supermemory-consumer-last-org-slug")

		if (savedSlug && orgs.find((o) => o.slug === savedSlug)) {
			setActiveOrg(savedSlug)
			return
		}

		if (savedSlug) localStorage.removeItem("supermemory-consumer-last-org-slug")
		authClient.organization.getFullOrganization().then(setOrg)
	}, [session?.session.activeOrganizationId, orgs])

	// When a session exists and there is a pending login method recorded,
	// promote it to the last-used method (successful login) and clear pending.
	useEffect(() => {
		if (typeof window === "undefined") return
		if (!session?.session) return

		try {
			const pendingMethod = localStorage.getItem(
				"supermemory-pending-login-method",
			)
			const pendingTsRaw = localStorage.getItem(
				"supermemory-pending-login-timestamp",
			)

			if (pendingMethod) {
				const now = Date.now()
				const ts = pendingTsRaw ? Number.parseInt(pendingTsRaw, 10) : Number.NaN
				const isFresh = Number.isFinite(ts) && now - ts < 10 * 60 * 1000 // 10 minutes TTL

				if (isFresh) {
					localStorage.setItem("supermemory-last-login-method", pendingMethod)
				}
			}
		} catch {}
		// Always clear pending markers once a session is present
		try {
			localStorage.removeItem("supermemory-pending-login-method")
			localStorage.removeItem("supermemory-pending-login-timestamp")
		} catch {}
	}, [session?.session])

	return (
		<AuthContext.Provider
			value={{
				org,
				session: session?.session ?? null,
				user: session?.user ?? null,
				setActiveOrg,
				updateOrgMetadata,
			}}
		>
			{children}
		</AuthContext.Provider>
	)
}

export function useAuth() {
	const context = useContext(AuthContext)
	if (context === undefined) {
		throw new Error("useAuth must be used within an AuthProvider")
	}
	return context
}
