import React, { createContext, useContext, useState, useEffect, useCallback, useRef, type ReactNode } from 'react';
import { api, setOnUnauthorizedCallback } from '@/services/api';
import type { User, LoginCredentials, ApiError } from '@/types';

interface AuthState {
  user: User | null;
  isLoading: boolean;
  isAuthenticated: boolean;
  error: ApiError | null;
}

interface AuthContextType extends AuthState {
  login: (credentials: LoginCredentials) => Promise<boolean>;
  logout: () => Promise<void>;
  clearError: () => void;
}

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

export function AuthProvider({ children }: { children: ReactNode }) {
  const [state, setState] = useState<AuthState>({
    user: null,
    isLoading: true,
    isAuthenticated: false,
    error: null,
  });

  // Check authentication on mount
  useEffect(() => {
    checkAuth();
  }, []);

  // Set up callback for 401 responses - auto logout
  useEffect(() => {
    setOnUnauthorizedCallback(() => {
      api.logout().then(() => {
        setState({
          user: null,
          isLoading: false,
          isAuthenticated: false,
          error: { message: 'Session expired. Please login again.' },
        });
      });
    });
  }, []);

  const checkAuth = async () => {
    try {
      const isAuth = await api.isAuthenticated();
      if (isAuth) {
        const user = await api.getStoredUser();
        setState({
          user,
          isLoading: false,
          isAuthenticated: !!user,
          error: null,
        });
      } else {
        setState({
          user: null,
          isLoading: false,
          isAuthenticated: false,
          error: null,
        });
      }
    } catch (error) {
      setState({
        user: null,
        isLoading: false,
        isAuthenticated: false,
        error: { message: 'Failed to check authentication' },
      });
    }
  };

  const login = useCallback(async (credentials: LoginCredentials): Promise<boolean> => {
    setState((prev) => ({ ...prev, isLoading: true, error: null }));

    try {
      const response = await api.login(credentials.username, credentials.password);

      if (response.ok && response.data) {
        const user: User = {
          user_id: response.data.user_id,
          user_name: credentials.username,
          max_role: response.data.max_role,
          privileges: response.data.privileges,
        };

        setState({
          user,
          isLoading: false,
          isAuthenticated: true,
          error: null,
        });

        return true;
      }

      setState((prev) => ({
        ...prev,
        isLoading: false,
        error: response.error || { message: 'Login failed' },
      }));

      return false;
    } catch (error) {
      setState((prev) => ({
        ...prev,
        isLoading: false,
        error: { message: error instanceof Error ? error.message : 'Login failed' },
      }));

      return false;
    }
  }, []);

  const logout = useCallback(async () => {
    setState((prev) => ({ ...prev, isLoading: true }));

    try {
      await api.logout();
    } finally {
      setState({
        user: null,
        isLoading: false,
        isAuthenticated: false,
        error: null,
      });
    }
  }, []);

  const clearError = useCallback(() => {
    setState((prev) => ({ ...prev, error: null }));
  }, []);

  return (
    <AuthContext.Provider value={{ ...state, login, logout, clearError }}>
      {children}
    </AuthContext.Provider>
  );
}

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