// Auto-generated by DjangoCFG - see CLAUDE.md
/**
 * Typed fetchers for Oauth
 *
 * Universal functions that work in any environment:
 * - Next.js (App Router / Pages Router / Server Components)
 * - React Native
 * - Node.js backend
 *
 * These fetchers use Zod schemas for runtime validation.
 *
 * Usage:
 * ```typescript
 * // Configure API once (in your app entry point)
 * import { configureAPI } from '../../api-instance'
 * configureAPI({ baseUrl: 'https://api.example.com' })
 *
 * // Then use fetchers anywhere
 * const users = await getUsers({ page: 1 })
 *
 * // With SWR
 * const { data } = useSWR(['users', params], () => getUsers(params))
 *
 * // With React Query
 * const { data } = useQuery(['users', params], () => getUsers(params))
 *
 * // In Server Component or SSR (pass custom client)
 * import { API } from '../../index'
 * const api = new API('https://api.example.com')
 * const users = await getUsers({ page: 1 }, api)
 * ```
 */
import { consola } from 'consola'
import { OAuthAuthorizeRequestRequestSchema, type OAuthAuthorizeRequestRequest } from '../schemas/OAuthAuthorizeRequestRequest.schema'
import { OAuthAuthorizeResponseSchema, type OAuthAuthorizeResponse } from '../schemas/OAuthAuthorizeResponse.schema'
import { OAuthCallbackRequestRequestSchema, type OAuthCallbackRequestRequest } from '../schemas/OAuthCallbackRequestRequest.schema'
import { OAuthDisconnectRequestRequestSchema, type OAuthDisconnectRequestRequest } from '../schemas/OAuthDisconnectRequestRequest.schema'
import { OAuthProvidersResponseSchema, type OAuthProvidersResponse } from '../schemas/OAuthProvidersResponse.schema'
import { OAuthTokenResponseSchema, type OAuthTokenResponse } from '../schemas/OAuthTokenResponse.schema'
import { getAPIInstance } from '../../api-instance'

/**
 * List OAuth connections
 *
 * @method GET
 * @path /cfg/accounts/oauth/connections/
 */
export async function getAccountsOauthConnectionsList(  client?: any
): Promise<any> {
  const api = client || getAPIInstance()
  const response = await api.oauth.accountsOauthConnectionsList()
  return response
}


/**
 * Disconnect OAuth provider
 *
 * @method POST
 * @path /cfg/accounts/oauth/disconnect/
 */
export async function createAccountsOauthDisconnectCreate(  data: OAuthDisconnectRequestRequest,  client?: any
): Promise<any> {
  const api = client || getAPIInstance()
  const response = await api.oauth.accountsOauthDisconnectCreate(data)
  return response
}


/**
 * Start GitHub OAuth
 *
 * @method POST
 * @path /cfg/accounts/oauth/github/authorize/
 */
export async function createAccountsOauthGithubAuthorizeCreate(  data: OAuthAuthorizeRequestRequest,  client?: any
): Promise<OAuthAuthorizeResponse> {
  const api = client || getAPIInstance()
  const response = await api.oauth.accountsOauthGithubAuthorizeCreate(data)
  try {
    return OAuthAuthorizeResponseSchema.parse(response)
  } catch (error) {
    // Zod validation error - log detailed information
    consola.error('❌ Zod Validation Failed');
    consola.box(`createAccountsOauthGithubAuthorizeCreate\nPath: /cfg/accounts/oauth/github/authorize/\nMethod: POST`);

    if (error instanceof Error && 'issues' in error && Array.isArray((error as any).issues)) {
      consola.error('Validation Issues:');
      (error as any).issues.forEach((issue: any, index: number) => {
        consola.error(`  ${index + 1}. ${issue.path.join('.') || 'root'}`);
        consola.error(`     ├─ Message: ${issue.message}`);
        if (issue.expected) consola.error(`     ├─ Expected: ${issue.expected}`);
        if (issue.received) consola.error(`     └─ Received: ${issue.received}`);
      });
    }

    consola.error('Response data:', response);

    // Dispatch browser CustomEvent (only if window is defined)
    if (typeof window !== 'undefined' && error instanceof Error && 'issues' in error) {
      try {
        const event = new CustomEvent('zod-validation-error', {
          detail: {
            operation: 'createAccountsOauthGithubAuthorizeCreate',
            path: '/cfg/accounts/oauth/github/authorize/',
            method: 'POST',
            error: error,
            response: response,
            timestamp: new Date(),
          },
          bubbles: true,
          cancelable: false,
        });
        window.dispatchEvent(event);
      } catch (eventError) {
        // Silently fail - event dispatch should never crash the app
        consola.warn('Failed to dispatch validation error event:', eventError);
      }
    }

    // Re-throw the error
    throw error;
  }
}


/**
 * Complete GitHub OAuth
 *
 * @method POST
 * @path /cfg/accounts/oauth/github/callback/
 */
export async function createAccountsOauthGithubCallbackCreate(  data: OAuthCallbackRequestRequest,  client?: any
): Promise<OAuthTokenResponse> {
  const api = client || getAPIInstance()
  const response = await api.oauth.accountsOauthGithubCallbackCreate(data)
  try {
    return OAuthTokenResponseSchema.parse(response)
  } catch (error) {
    // Zod validation error - log detailed information
    consola.error('❌ Zod Validation Failed');
    consola.box(`createAccountsOauthGithubCallbackCreate\nPath: /cfg/accounts/oauth/github/callback/\nMethod: POST`);

    if (error instanceof Error && 'issues' in error && Array.isArray((error as any).issues)) {
      consola.error('Validation Issues:');
      (error as any).issues.forEach((issue: any, index: number) => {
        consola.error(`  ${index + 1}. ${issue.path.join('.') || 'root'}`);
        consola.error(`     ├─ Message: ${issue.message}`);
        if (issue.expected) consola.error(`     ├─ Expected: ${issue.expected}`);
        if (issue.received) consola.error(`     └─ Received: ${issue.received}`);
      });
    }

    consola.error('Response data:', response);

    // Dispatch browser CustomEvent (only if window is defined)
    if (typeof window !== 'undefined' && error instanceof Error && 'issues' in error) {
      try {
        const event = new CustomEvent('zod-validation-error', {
          detail: {
            operation: 'createAccountsOauthGithubCallbackCreate',
            path: '/cfg/accounts/oauth/github/callback/',
            method: 'POST',
            error: error,
            response: response,
            timestamp: new Date(),
          },
          bubbles: true,
          cancelable: false,
        });
        window.dispatchEvent(event);
      } catch (eventError) {
        // Silently fail - event dispatch should never crash the app
        consola.warn('Failed to dispatch validation error event:', eventError);
      }
    }

    // Re-throw the error
    throw error;
  }
}


/**
 * List OAuth providers
 *
 * @method GET
 * @path /cfg/accounts/oauth/providers/
 */
export async function getAccountsOauthProvidersRetrieve(  client?: any
): Promise<OAuthProvidersResponse> {
  const api = client || getAPIInstance()
  const response = await api.oauth.accountsOauthProvidersRetrieve()
  try {
    return OAuthProvidersResponseSchema.parse(response)
  } catch (error) {
    // Zod validation error - log detailed information
    consola.error('❌ Zod Validation Failed');
    consola.box(`getAccountsOauthProvidersRetrieve\nPath: /cfg/accounts/oauth/providers/\nMethod: GET`);

    if (error instanceof Error && 'issues' in error && Array.isArray((error as any).issues)) {
      consola.error('Validation Issues:');
      (error as any).issues.forEach((issue: any, index: number) => {
        consola.error(`  ${index + 1}. ${issue.path.join('.') || 'root'}`);
        consola.error(`     ├─ Message: ${issue.message}`);
        if (issue.expected) consola.error(`     ├─ Expected: ${issue.expected}`);
        if (issue.received) consola.error(`     └─ Received: ${issue.received}`);
      });
    }

    consola.error('Response data:', response);

    // Dispatch browser CustomEvent (only if window is defined)
    if (typeof window !== 'undefined' && error instanceof Error && 'issues' in error) {
      try {
        const event = new CustomEvent('zod-validation-error', {
          detail: {
            operation: 'getAccountsOauthProvidersRetrieve',
            path: '/cfg/accounts/oauth/providers/',
            method: 'GET',
            error: error,
            response: response,
            timestamp: new Date(),
          },
          bubbles: true,
          cancelable: false,
        });
        window.dispatchEvent(event);
      } catch (eventError) {
        // Silently fail - event dispatch should never crash the app
        consola.warn('Failed to dispatch validation error event:', eventError);
      }
    }

    // Re-throw the error
    throw error;
  }
}


