import {type User} from 'common/user'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import {getAuth, GoogleAuthProvider, signInWithCredential, signInWithPopup} from 'firebase/auth'

import {safeLocalStorage} from '../util/local'
import {app} from './init'
import {GOOGLE_CLIENT_ID} from "common/constants"
import {isAndroidWebView} from "web/lib/util/webview"
import {SocialLogin} from "@capgo/capacitor-social-login"
import {Capacitor} from "@capacitor/core"

dayjs.extend(utc)

export type {User}

export const auth = getAuth(app)

export const CACHED_REFERRAL_USERNAME_KEY = 'CACHED_REFERRAL_KEY'

// Scenarios:
// 1. User is referred by another user to homepage, group page, market page etc. explicitly via referrer= query param
// 2. User lands on a market or group without a referrer, we attribute the market/group creator
// Explicit referrers take priority over the implicit ones, (e.g. they're overwritten)
export function writeReferralInfo(
  defaultReferrerUsername: string,
  otherOptions?: {
    contractId?: string
    explicitReferrer?: string
  }
) {
  const local = safeLocalStorage
  const cachedReferralUser = local?.getItem(CACHED_REFERRAL_USERNAME_KEY)
  const {explicitReferrer} = otherOptions || {}

  // Write the first referral username we see.
  if (!cachedReferralUser) {
    local?.setItem(
      CACHED_REFERRAL_USERNAME_KEY,
      explicitReferrer || defaultReferrerUsername
    )
  }

  // Overwrite all referral info if we see an explicit referrer.
  if (explicitReferrer) {
    local?.setItem(CACHED_REFERRAL_USERNAME_KEY, explicitReferrer)
  }
}

/**
 * Authenticates a Firebase client running a webview APK on Android with Google OAuth.
 *
 * Calls `https://accounts.google.com/o/oauth2/v2/auth?${params}` to get the code (in external browser, as Google blocks it in webview)
 * Redirects to `com.compassmeet://auth` (in webview java main activity), which triggers oauthRedirect in the app (see _app.tsx)
 * Calls backend endpoint `https://api.compassmeet.com/auth-google` to get the tokens from the code ('https://oauth2.googleapis.com/token')
 * Uses signInWithCredential(auth, credential) to set up firebase user in the client (auth.currentUser)
 *
 * Deprecated for SocialLogin with capacitor, which is native and faster
 *
 * @public
 */
// export async function webviewGoogleSignin() {
//   const params = {
//     client_id: GOOGLE_CLIENT_ID,
//     redirect_uri: REDIRECT_URI,
//     response_type: 'code',
//     scope: 'openid email profile',
//   }
//   console.log('params', params)
//   window.open(`https://accounts.google.com/o/oauth2/v2/auth?${new URLSearchParams(params)}`, '_system')
// }

/**
 * Authenticates a Firebase client running a webview APK on Android with native Google OAuth.
 *
 * @public
 */
export async function googleNativeLogin() {
  console.log('Platform:', Capacitor.getPlatform())
  console.log('URL origin:', window.location.origin)

  await SocialLogin.initialize({google: {webClientId: GOOGLE_CLIENT_ID}})

  // Run the native Google OAuth
  const {result}: any = await SocialLogin.login({provider: 'google', options: {}})

  console.log('SocialLogin.login result:', JSON.stringify(result))

  // Extract the tokens from the native result
  const idToken = result?.idToken
  const accessToken = result?.accessToken?.token

  if (!idToken) {
    throw new Error('No idToken returned from Google login')
  }

  // Create a Firebase credential from the Google tokens
  const credential = GoogleAuthProvider.credential(idToken, accessToken)

  // Sign in with Firebase using the credential
  const userCredential = await signInWithCredential(auth, credential)

  console.log('Firebase user:', userCredential.user)

  return userCredential
}

export async function firebaseLogin() {
  if (isAndroidWebView()) {
    console.log('Running in APK')
    return await googleNativeLogin()
  }
  console.log('Running in web')
  const provider = new GoogleAuthProvider()
  return signInWithPopup(auth, provider).then(async (result) => {
    return result
  })
}

// export async function loginWithApple() {
//   const provider = new OAuthProvider('apple.com')
//   provider.addScope('email')
//   provider.addScope('name')
//
//   return signInWithPopup(auth, provider)
//     .then((result) => {
//       return result
//     })
//     .catch((error) => {
//       console.error(error)
//     })
// }

export async function firebaseLogout() {
  await auth.signOut()
}
