'use server'
import { cookies } from "next/headers";

import LoginForm            from "@/components/client/admin/loginForm";
import ClientAuthHandler    from "@/components/client/admin/clientAuthHandler";

import { serverValidateSessionCookie } from "@/app/lib/actions/actions";
import { ReactNode } from "react";
import { AuthContext, AuthProps } from "@/providers/providers";


interface Props {
    children?: ReactNode;
    params?: any;
    requiredRole?: number;
} // We interfacing lads


export default async function AuthHandler(props: Props) {
    const protoKoek = await cookies().get('auth')?.value;
    const koek = decodeURIComponent(protoKoek ? protoKoek: "");
    // console.log("koekje:" + koek)
    let p:AuthProps = {
    };
    if(koek){
        const kd = JSON.parse(koek);
        if(kd.id && kd.token && kd.user_id){
            p = {
                auth: {
                    id:kd.id,
                    token:kd.token,
                    user_id:kd.user_id
                }
            }
        }
    }

    return (
        <div className="flex flex-row w-[100%] h-[100%]">
            {!(koek && await serverValidateSessionCookie(koek)) ? <LoginForm>{ }</LoginForm> : <ClientAuthHandler authProps={p}>{props.children}</ClientAuthHandler>}
        </div>
    );
}
