'use client';

import "./style.scss";
import Image from 'next/image';
import Link from 'next/link';
import { useRouter, usePathname } from 'next/navigation';
import { useEffect } from 'react';
import { GoogleOAuthProvider } from '@react-oauth/google';
import useAuth from '@/hooks/useAuth';
import { useConfigContext } from '@/contexts/configProvider';

export default function Layout({ children }: { children: React.ReactNode }) {
    const router = useRouter();
    const pathname = usePathname();
    const { isAuthenticated, isLoading } = useAuth();
    const config = useConfigContext();

    // 이미 로그인된 상태면 홈으로 리다이렉트 (Google 로그인 완료 페이지 제외)
    useEffect(() => {
        if (!isLoading && isAuthenticated && !pathname.startsWith('/login/google/complete')) {
            router.replace('/');
        }
    }, [isAuthenticated, isLoading, router, pathname]);

    return (
        <div className="grid grid-rows-[1fr_20px] items-center justify-items-center min-h-screen p-4 sm:p-20 font-[family-name:var(--font-geist-sans)]">
            <div className="flex flex-col items-center gap-10">
                <Link href="/" className="inline-block w-28 sm:w-30 md:w-36 lg:w-44">
                    <Image src="/resources/m-logo.png" alt="bitforum" width={256} height={64} className="w-full h-auto" priority />
                </Link>
                <GoogleOAuthProvider clientId={config?.external?.googleClientId ?? ''} nonce="" locale="ko">
                    {children}
                </GoogleOAuthProvider>
            </div>
            <address>
                <hr />
                <small>© 2025 PLAYR. All rights reserved.</small>
            </address>
        </div>
    );
}