import { createContext, useContext, useState, useEffect, type ReactNode } from "react";
import { logout as apiLogout } from "@/lib/api";

interface AuthCtx {
  isAuthenticated: boolean;
  isLoading: boolean;
  logout: () => Promise<void>;
  setAuthenticated: (v: boolean) => void;
}

const AuthContext = createContext<AuthCtx>({
  isAuthenticated: false,
  isLoading: true,
  logout: async () => {},
  setAuthenticated: () => {},
});

export function AuthProvider({ children }: { children: ReactNode }) {
  const [isAuthenticated, setAuthenticated] = useState(false);
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    // Probe auth state by hitting a protected endpoint.
    // If the ws_token cookie is valid, the server returns 200; else 401.
    fetch("/api/v1/auth/me", { credentials: "include" })
      .then((r) => setAuthenticated(r.ok))
      .catch(() => setAuthenticated(false))
      .finally(() => setIsLoading(false));
  }, []);

  const logout = async () => {
    await apiLogout();
    setAuthenticated(false);
  };

  return (
    <AuthContext.Provider value={{ isAuthenticated, isLoading, logout, setAuthenticated }}>
      {children}
    </AuthContext.Provider>
  );
}

export const useAuth = () => useContext(AuthContext);
