import React from 'react';

import type { User } from '../../';
import type { OTPRequestResult } from '../types';

// User profile type
export type UserProfile = User;

// Auth configuration
export interface AuthConfig {
  apiUrl?: string;
  routes?: {
    auth?: string;
    defaultCallback?: string;
    defaultAuthCallback?: string;
  };
  onLogout?: () => void;
}

/**
 * AuthConfig.routes resolved against the framework conventions — no field is
 * optional here. This is what components consume (via `useAuth().routes`)
 * instead of defaulting to route constants themselves.
 */
export interface ResolvedAuthRoutes {
  auth: string;
  defaultCallback: string;
  defaultAuthCallback: string;
}

// Auth context interface
export interface AuthContextType {
  user: UserProfile | null;
  isLoading: boolean;
  isAuthenticated: boolean;
  isAdminUser: boolean;
  /** Resolved route conventions (config override or framework default). */
  routes: ResolvedAuthRoutes;
  loadCurrentProfile: (callerId?: string) => Promise<void>;
  checkAuthAndRedirect: () => Promise<void>;

  // Token Methods
  getToken: () => string | null;
  getRefreshToken: () => string | null;

  // Email Methods
  getSavedEmail: () => string | null;
  saveEmail: (email: string) => void;
  clearSavedEmail: () => void;

  // OTP Methods - email only
  requestOTP: (identifier: string, sourceUrl?: string) => Promise<OTPRequestResult>;
  verifyOTP: (identifier: string, otpCode: string, sourceUrl?: string, redirectUrl?: string, skipRedirect?: boolean) => Promise<{
    success: boolean;
    message: string;
    user?: UserProfile;
    requires_2fa?: boolean;
    session_id?: string | null;
    should_prompt_2fa?: boolean;
  }>;
  refreshToken: () => Promise<{ success: boolean; message: string }>;
  logout: () => void;

  // Redirect URL Methods - for saving URL before auth redirect
  saveRedirectUrl: (url: string) => void;
  getRedirectUrl: () => string;
  clearRedirectUrl: () => void;
  hasRedirectUrl: () => boolean;

  // Profile Management Methods
  updateProfile: (data: { first_name?: string; last_name?: string; username?: string }) => Promise<UserProfile>;
  uploadAvatar: (avatar: File | Blob) => Promise<UserProfile>;
}

// Provider props
export interface AuthProviderProps {
  children: React.ReactNode;
  config?: AuthConfig;
  /** Set false to skip all auth logic and render children directly. */
  enabled?: boolean;
}
