"use client";
import React, {createContext, useContext} from "react";
import {Client, Account} from "appwrite";


export type AppContextType = {
    signup: (name: string, email: string, password: string) => Promise<void>;
    login: (email: string, password: string) => Promise<void>;
}

const AppContext = createContext<AppContextType | null>(null)


const Provider: React.FC<{ children: React.ReactNode }> = ({children}) => {
    // initialize app-write project
    const client = new Client()
        .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!)
        .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT!)

    const account = new Account(client)

    // login functionality
    const login = async (email: string, password: string) => {
        try {
            const session = await account.createEmailPasswordSession(email, password);
        } catch (e) {
            console.log('login errors', e)
        }
    }

    const signup = async (name: string, email: string, password: string) => {
        // await account.create(ID.unique(), email, password, name);

        // cookies().set(SESSION_KEY, session.secret, {
        //     path: "/",
        //     httpOnly: true,
        //     sameSite: "strict",
        //     secure: true,
        // });
    }

    const exposedValues = {
        signup,
        login
    }

    return <AppContext.Provider value={exposedValues}>{children}</AppContext.Provider>
}


export default Provider;
export const useAppContext = () => useContext(AppContext) as AppContextType