'use client';

import React, { createContext, useContext, ReactNode } from 'react';
import { useAuth } from './useAuth';
import type { AuthState, LoginRequest, RegisterRequest, User } from '@/entities/user';

interface AuthContextType extends AuthState {
  login: (credentials: LoginRequest) => Promise<{
    success: boolean;
    user?: User;
    error?: string;
  }>;
  register: (data: RegisterRequest) => Promise<{
    success: boolean;
    user?: User;
    error?: string;
  }>;
  logout: () => Promise<void>;
  refreshProfile: () => Promise<{
    success: boolean;
    user?: User;
    error?: string;
  }>;
}

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

export const AuthProvider = ({ children }: { children: ReactNode }) => {
  const auth = useAuth();

  return <AuthContext.Provider value={auth}>{children}</AuthContext.Provider>;
};

export const useAuthContext = () => {
  const context = useContext(AuthContext);
  if (!context) {
    throw new Error('useAuthContext must be used within AuthProvider');
  }
  return context;
};
