import {Account, Avatars, Client, OAuthProvider} from "react-native-appwrite";
import * as Linking from "expo-linking"
import {openAuthSessionAsync} from "expo-web-browser";

export const config = {
    platform: 'com.placebomk.mobilemk',
    endpoint: process.env.EXPO_PUBLIC_APPWRITE_ENDPOINT,
    projectId: process.env.EXPO_PUBLIC_APPWRITE_PROJECT_ID,
}

export const client = new Client();

client
  .setEndpoint(config.endpoint!)
  .setProject(config.projectId!)
    .setPlatform(config.platform!)

export const avatar = new Avatars(client);
export const account = new Account(client);


export async function login (){
    // Your code here to authenticate the user and return their JWT token.
    try {
        const redirectUri = Linking.createURL('/');
        const response = await account.createOAuth2Token(OAuthProvider.Google, redirectUri);

        if(!response) throw new Error('failed to login');

        const browserResult = await openAuthSessionAsync(
            response.toString(),
            redirectUri
        )

        if (!browserResult.type || browserResult.type !== 'success') {
            throw new Error('failed to log in');
        }

        const url = new URL(browserResult.url);

        const secret = url.searchParams.get('secret')?.toString();
        const userId = url.searchParams.get('userId')?.toString();

        if (!secret ||!userId) {
            throw new Error('failed to authenticate user');
        }

        const session = await account.createSession(userId, secret);
       if (!session) throw new Error('failed to create session');
        return true;
    }
    catch (error) {
        console.error("Failed to authenticate user:", error);
        return false;
    }
}

export async function getCurrentUser() {
    try {
        const response = await account.get();
        if(response.$id){
            const userAvatar = avatar.getInitials(response.name)
            return {
                ...response,
                avatar: userAvatar.toString(),
            }
        }
    } catch (error) {
        console.error(error)
        return null;
    }
}

export async function logOut(): Promise<boolean> {
    try {
        const result = await account.deleteSession('current');
        return result
    } catch (error) {
        console.error("Failed to log out user:", error);
        return false;
    }
}