import { createContext, useContext, useEffect, useState } from 'react';
import { api } from '../api.js';

const AuthCtx = createContext(null);

export function AuthProvider({ children }) {
  const [token, setToken] = useState(() => localStorage.getItem('cl_token'));
  const [user, setUser] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    if (!token) { setLoading(false); return; }
    api.get('/auth/me')
      .then(({ user }) => setUser(user))
      .catch(() => logout())
      .finally(() => setLoading(false));
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [token]);

  const login = async (email, password) => {
    const r = await api.post('/auth/login', { email, password });
    localStorage.setItem('cl_token', r.token);
    setToken(r.token);
    setUser(r.user);
    return r.user;
  };

  const register = async (email, password, displayName) => {
    const r = await api.post('/auth/register', { email, password, displayName });
    localStorage.setItem('cl_token', r.token);
    setToken(r.token);
    setUser(r.user);
    return r.user;
  };

  const logout = () => {
    localStorage.removeItem('cl_token');
    localStorage.removeItem('cl_investisseur_id');
    setToken(null);
    setUser(null);
  };

  const updateUser = async (payload) => {
    const r = await api.put('/auth/me', payload);
    setUser(r.user);
    // If backend issued a fresh token (email changed), persist it
    if (r.token) {
      localStorage.setItem('cl_token', r.token);
      setToken(r.token);
    }
    return r.user;
  };

  const isAdmin = user?.role === 'admin';

  return (
    <AuthCtx.Provider value={{ token, user, loading, login, register, logout, updateUser, isAdmin }}>
      {children}
    </AuthCtx.Provider>
  );
}

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