import Image from "next/image";
import { signout } from "@/features/auth/actions";
import { createClient } from "@/utils/supabase/server";
import { Button } from "@/components/ui/button";

/**
 * [메인 페이지 컴포넌트]
 *
 * 로그인한 사용자만 접근 가능 (Middleware에서 보호)
 * - 사용자 정보 표시 (이메일, 프로필 아바타)
 * - 로그아웃 버튼 제공
 */
export default async function Home() {
  // 현재 로그인한 사용자 정보 가져오기
  // Middleware에서 이미 인증을 확인했으므로 여기서는 user가 항상 존재함
  const supabase = await createClient();
  const {
    data: { user },
  } = await supabase.auth.getUser();

  return (
    <div className="flex min-h-screen items-center justify-center bg-zinc-50 font-sans dark:bg-black">
      <main className="flex min-h-screen w-full max-w-3xl flex-col items-center justify-between py-32 px-16 bg-white dark:bg-black sm:items-start">
        {/* ========== 헤더: 로그인 정보 및 로그아웃 버튼 ========== */}
        <div className="flex w-full items-center justify-between">
          {/* 사용자 프로필 표시 */}
          <div className="flex items-center gap-3">
            {/* 프로필 아바타: 이메일 첫 글자 표시 */}
            <div className="flex h-10 w-10 items-center justify-center rounded-full bg-gradient-to-br from-indigo-500 to-purple-600 text-white font-semibold">
              {user?.email?.charAt(0).toUpperCase() || "U"}
            </div>
            {/* 이메일 및 로그인 상태 텍스트 */}
            <div>
              <p className="text-sm font-medium text-gray-900 dark:text-white">
                {user?.email || "사용자"}
              </p>
              <p className="text-xs text-gray-500 dark:text-gray-400">
                로그인됨
              </p>
            </div>
          </div>
          {/* 로그아웃 폼 */}
          {/* formAction: 서버 액션(signout)을 호출하여 로그아웃 처리 */}
          <form>
            <Button
              formAction={signout}
              variant="outline"
              size="sm"
              className="text-sm"
            >
              로그아웃
            </Button>
          </form>
        </div>

        <Image
          className="dark:invert"
          src="/next.svg"
          alt="Next.js logo"
          width={100}
          height={20}
          priority
        />
        <div className="flex flex-col items-center gap-6 text-center sm:items-start sm:text-left">
          <h1 className="max-w-xs text-3xl font-semibold leading-10 tracking-tight text-black dark:text-zinc-50">
            To get started, edit the page.tsx file.
          </h1>
          <p className="max-w-md text-lg leading-8 text-zinc-600 dark:text-zinc-400">
            Looking for a starting point or more instructions? Head over to{" "}
            <a
              href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
              className="font-medium text-zinc-950 dark:text-zinc-50"
            >
              Templates
            </a>{" "}
            or the{" "}
            <a
              href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
              className="font-medium text-zinc-950 dark:text-zinc-50"
            >
              Learning
            </a>{" "}
            center.
          </p>
        </div>
        <div className="flex flex-col gap-4 text-base font-medium sm:flex-row">
          <a
            className="flex h-12 w-full items-center justify-center gap-2 rounded-full bg-foreground px-5 text-background transition-colors hover:bg-[#383838] dark:hover:bg-[#ccc] md:w-[158px]"
            href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
            target="_blank"
            rel="noopener noreferrer"
          >
            <Image
              className="dark:invert"
              src="/vercel.svg"
              alt="Vercel logomark"
              width={16}
              height={16}
            />
            Deploy Now
          </a>
          <a
            className="flex h-12 w-full items-center justify-center rounded-full border border-solid border-black/[.08] px-5 transition-colors hover:border-transparent hover:bg-black/[.04] dark:border-white/[.145] dark:hover:bg-[#1a1a1a] md:w-[158px]"
            href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
            target="_blank"
            rel="noopener noreferrer"
          >
            Documentation
          </a>
        </div>
      </main>
    </div>
  );
}
