"use client";

import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import Link from "next/link";

interface User {
  id: string;
  name: string;
  email: string;
  emailVerified: boolean;
  image: string | null;
  createdAt: string;
  updatedAt: string;
}

export default function ProfilePage() {
  const [user, setUser] = useState<User | null>(null);
  const [loading, setLoading] = useState(true);
  const router = useRouter();

  useEffect(() => {
    const fetchUser = async () => {
      try {
        const response = await fetch("/api/auth/get-session", {
          credentials: "include",
        });
        const data = await response.json();

        if (!response.ok || !data.user) {
          router.push("/login");
          return;
        }

        setUser(data.user);
      } catch (error) {
        console.error("Kullanıcı bilgileri alınamadı:", error);
        router.push("/login");
      } finally {
        setLoading(false);
      }
    };

    fetchUser();
  }, [router]);

  const handleLogout = async () => {
    try {
      await fetch("/api/auth/sign-out", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        credentials: "include",
        body: JSON.stringify({}),
      });
      router.push("/login");
      router.refresh();
    } catch (error) {
      console.error("Çıkış yapılamadı:", error);
    }
  };

  if (loading) {
    return (
      <div className="flex min-h-screen items-center justify-center bg-zinc-50 dark:bg-black">
        <div className="text-lg text-gray-600 dark:text-gray-400">
          Yükleniyor...
        </div>
      </div>
    );
  }

  if (!user) {
    return null;
  }

  return (
    <div className="min-h-screen bg-zinc-50 dark:bg-black">
      <div className="mx-auto max-w-4xl px-4 py-16">
        <div className="rounded-lg bg-white p-8 shadow-lg dark:bg-zinc-900">
          <div className="mb-6 flex items-center justify-between">
            <h1 className="text-3xl font-bold text-black dark:text-zinc-50">
              Kullanıcı Bilgileri
            </h1>
            <div className="flex gap-4">
              <Link
                href="/upload"
                className="rounded-md bg-blue-600 px-4 py-2 text-white hover:bg-blue-700"
              >
                Resim Yükle
              </Link>
              <Link
                href="/"
                className="rounded-md bg-gray-200 px-4 py-2 text-gray-700 hover:bg-gray-300 dark:bg-zinc-800 dark:text-zinc-300 dark:hover:bg-zinc-700"
              >
                Ana Sayfa
              </Link>
              <button
                onClick={handleLogout}
                className="rounded-md bg-red-600 px-4 py-2 text-white hover:bg-red-700"
              >
                Çıkış Yap
              </button>
            </div>
          </div>

          <div className="space-y-6">
            <div className="rounded-md border border-gray-200 p-6 dark:border-zinc-700">
              <h2 className="mb-4 text-xl font-semibold text-black dark:text-zinc-50">
                Profil Bilgileri
              </h2>
              <dl className="space-y-4">
                <div>
                  <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                    Ad Soyad
                  </dt>
                  <dd className="mt-1 text-lg text-black dark:text-zinc-50">
                    {user.name}
                  </dd>
                </div>
                <div>
                  <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                    E-posta
                  </dt>
                  <dd className="mt-1 text-lg text-black dark:text-zinc-50">
                    {user.email}
                  </dd>
                </div>
                <div>
                  <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                    E-posta Doğrulandı mı?
                  </dt>
                  <dd className="mt-1 text-lg text-black dark:text-zinc-50">
                    {user.emailVerified ? (
                      <span className="text-green-600 dark:text-green-400">
                        Evet
                      </span>
                    ) : (
                      <span className="text-red-600 dark:text-red-400">
                        Hayır
                      </span>
                    )}
                  </dd>
                </div>
                <div>
                  <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                    Kullanıcı ID
                  </dt>
                  <dd className="mt-1 text-sm text-gray-600 dark:text-gray-400 font-mono">
                    {user.id}
                  </dd>
                </div>
                <div>
                  <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                    Kayıt Tarihi
                  </dt>
                  <dd className="mt-1 text-lg text-black dark:text-zinc-50">
                    {new Date(user.createdAt).toLocaleString("tr-TR")}
                  </dd>
                </div>
                <div>
                  <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                    Son Güncelleme
                  </dt>
                  <dd className="mt-1 text-lg text-black dark:text-zinc-50">
                    {new Date(user.updatedAt).toLocaleString("tr-TR")}
                  </dd>
                </div>
              </dl>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
