'use client';

import {
  createContext,
  useCallback,
  useContext,
  useMemo,
  type ReactNode,
} from 'react';
import { useRouter } from 'next/navigation';

import type { SessionViewModel } from '@/lib/view-models/SessionViewModel';
import { useCurrentSession } from "@/hooks/auth/useCurrentSession";
import { useLogout } from "@/hooks/auth/useLogout";

export type AuthContextValue = {
  session: SessionViewModel | null;
  loading: boolean;
  login: (returnTo?: string) => void;
  logout: () => Promise<void>;
  refreshSession: () => Promise<void>;
};

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

interface AuthProviderProps {
  initialSession?: SessionViewModel | null;
  children: ReactNode;
}

export function AuthProvider({ initialSession = null, children }: AuthProviderProps) {
  const router = useRouter();
  
  // Use React-Query hooks for session management
  const { data: session, isLoading, refetch: refreshSession } = useCurrentSession({
    initialData: initialSession,
  });

  // Use mutation hooks for logout
  const logoutMutation = useLogout();

  const login = useCallback(
    (returnTo?: string) => {
      const search = new URLSearchParams();
      if (returnTo) {
        search.set('returnTo', returnTo);
      }

      const target = search.toString()
        ? `/auth/login?${search.toString()}`
        : '/auth/login';

      router.push(target);
    },
    [router],
  );

  const logout = useCallback(async () => {
    try {
      await logoutMutation.mutateAsync();
      router.push('/');
      router.refresh();
    } catch (error) {
      console.error('Logout failed:', error);
      router.push('/');
    }
  }, [logoutMutation, router]);

  const handleRefreshSession = useCallback(async () => {
    await refreshSession();
  }, [refreshSession]);

  const value = useMemo(
    () => ({
      session: session ?? null,
      loading: isLoading,
      login,
      logout,
      refreshSession: handleRefreshSession,
    }),
    [session, isLoading, login, logout, handleRefreshSession],
  );

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

export function useAuth(): AuthContextValue {
  const ctx = useContext(AuthContext);
  if (!ctx) {
    throw new Error('useAuth must be used within an AuthProvider');
  }
  return ctx;
}