import { useState } from 'react'
import type { Feedback, SDKConfig } from '../interfaces'

export const useInsights = () => {
  const [isLoading, setIsLoading] = useState<boolean>(false)
  const [error, setError] = useState<string | null>(null)

  const sendFeedback = async (
    feedback: Feedback
  ): Promise<{ success: boolean; message: string }> => {
    setIsLoading(true)
    setError(null)
    try {
      //TODO: Call native modules when available
      console.log('sendFeedback', feedback)
      return { success: true, message: 'Feedback sent successfully' }
    } catch (error) {
      const errorMessage =
        error instanceof Error
          ? error.message
          : 'Failed to send feedback due to an unknown error'
      setError(errorMessage)
      return { success: false, message: errorMessage }
    } finally {
      setIsLoading(false)
    }
  }

  const sendTrackingEvent = async (event: string) => {
    //TODO: implement
    //TODO: send correct type of event
    console.log('trackEvent', event)
  }

  const sendTrackingEventOnNavigate = async (options: {
    screenName: string;
  }) => {
    console.log('sendEventOnNavigate', options.screenName)
    sendTrackingEvent(options.screenName)
  }

  const initializeSDK = async (configuration: SDKConfig) => {
    //TODO: implement
    //TODO: pass apiKey to native modules when available
    console.log('initializeSDK', configuration)
  }

  return {
    isLoading,
    error,
    sendFeedback,
    sendTrackingEvent,
    sendTrackingEventOnNavigate,
    initializeSDK,
  }
}
