import {
	createContext,
	type ReactNode,
	useCallback,
	useContext,
	useEffect,
	useState,
} from "react";
import { client } from "../lib/client";

interface User {
	id: string;
	name: string;
	email: string | null;
	isAdmin: boolean;
	personalToken: string;
}

interface AuthContextType {
	user: User | null;
	loading: boolean;
	login: () => void;
	logout: () => void;
	checkAuth: () => Promise<void>;
}

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

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

	const checkAuth = useCallback(async () => {
		try {
			const res = await client.api.auth.me.$get(undefined, {
				init: { credentials: "include" },
			});
			if (res.ok) {
				const data = await res.json();
				setUser(data.user);
			} else {
				setUser(null);
			}
		} catch (error) {
			console.error("Auth check failed:", error);
			setUser(null);
		} finally {
			setLoading(false);
		}
	}, []);

	useEffect(() => {
		checkAuth();
	}, [checkAuth]);

	const login = useCallback(async () => {
		try {
			const res = await client.api.auth["login-url"].$get(undefined, {
				init: { credentials: "include" },
			});
			const data = await res.json();
			window.location.href = data.loginUrl;
		} catch (error) {
			console.error("Login failed:", error);
		}
	}, []);

	const logout = useCallback(async () => {
		try {
			await client.api.auth.logout.$post(undefined, {
				init: { credentials: "include" },
			});
			setUser(null);
		} catch (error) {
			console.error("Logout failed:", error);
		}
	}, []);

	return (
		<AuthContext.Provider value={{ user, loading, login, logout, checkAuth }}>
			{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;
}
