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

// Test account for development - uses legacy anandk credentials
const DEV_EMAIL = 'serter2069@gmail.com';

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

interface OtpResult {
  success: boolean;
  skipOtp?: boolean;
}

interface AuthContextType extends AuthState {
  requestOtp: (email: string) => Promise<OtpResult>;
  verifyOtp: (email: string, code: string) => 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 requestOtp = useCallback(async (email: string): Promise<OtpResult> => {
    setState((prev) => ({ ...prev, isLoading: true, error: null }));

    try {
      // Check if dev email - skip OTP
      if (email.toLowerCase() === DEV_EMAIL.toLowerCase()) {
        setState((prev) => ({ ...prev, isLoading: false }));
        return { success: true, skipOtp: true };
      }

      // Send OTP via Brevo API
      const response = await api.requestOTP(email);

      if (response.ok) {
        setState((prev) => ({ ...prev, isLoading: false }));
        return { success: true, skipOtp: false };
      }

      // API failed
      setState((prev) => ({
        ...prev,
        isLoading: false,
        error: { message: 'Failed to send verification code. Please try again.' },
      }));
      return { success: false, skipOtp: false };
    } catch (error) {
      setState((prev) => ({
        ...prev,
        isLoading: false,
        error: { message: 'Network error. Please check your connection.' },
      }));
      return { success: false, skipOtp: false };
    }
  }, []);

  const verifyOtp = useCallback(async (email: string, code: string): Promise<boolean> => {
    setState((prev) => ({ ...prev, isLoading: true, error: null }));

    try {
      // Dev account bypass - use legacy credentials
      if (email.toLowerCase() === DEV_EMAIL.toLowerCase()) {
        // Login with legacy API using anandk credentials
        const response = await api.login('anandk', 'anandk_8');

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

          // Save email to storage
          await api.saveEmail(email);

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

          return true;
        }

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

      // Verify OTP via API
      const verifyResponse = await api.verifyOTP(email, code);

      if (verifyResponse.ok && verifyResponse.data) {
        const user: User = {
          user_id: verifyResponse.data.user.id,
          user_name: verifyResponse.data.user.first_name || email.split('@')[0],
          email: email,
          max_role: 'USER',
          privileges: [],
        };

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

        return true;
      }

      // Wrong OTP code
      setState((prev) => ({
        ...prev,
        isLoading: false,
        error: { message: 'Invalid verification code. Please try again.' },
      }));
      return false;
    } catch (error) {
      setState((prev) => ({
        ...prev,
        isLoading: false,
        error: { message: error instanceof Error ? error.message : 'Verification 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, requestOtp, verifyOtp, 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;
}
