"use client";

import { createContext, useContext, useEffect, useState } from "react";

interface AuthContextType {
  user: { email: string; uid: string } | null;
  loading: boolean;
  signInWithGoogle: () => Promise<void>;
  logout: () => Promise<void>;
}

const AuthContext = createContext<AuthContextType>({} as AuthContextType);

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

  useEffect(() => {
    // Simulation mode: Auto-login as guest after 1 second
    const timer = setTimeout(() => {
      setUser({ email: "guest@defrag.local", uid: "guest-simulation-mode" });
      setLoading(false);
    }, 1000);
    return () => clearTimeout(timer);
  }, []);

  const signInWithGoogle = async () => {
    // Simulation: instant guest login
    setUser({ email: "guest@defrag.local", uid: "guest-simulation-mode" });
  };

  const logout = async () => {
    setUser(null);
  };

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

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