'use client';

import "./style.scss";
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';
import Logo from '@/app/component/Logo';

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="auth-logo inline-block" aria-label="개미투자 홈">
                    <Logo size="lg" />
                </Link>
                <GoogleOAuthProvider clientId={config?.external?.googleClientId ?? ''} nonce="" locale="ko">
                    {children}
                </GoogleOAuthProvider>
            </div>
            <address>
                <hr />
                <small>© 2025 개미투자. All rights reserved.</small>
            </address>
        </div>
    );
}