import type { Metadata } from "next";
import { redirect } from "next/navigation";

import { getCurrentUser } from "@/lib/appwrite/server";
import { formatDateTime } from "@/lib/format";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";

import { NameForm } from "./components/name-form";
import { EmailForm } from "./components/email-form";
import { PasswordForm } from "./components/password-form";

export const metadata: Metadata = {
  title: "DLS — Profil",
};

export default async function AccountSettingsPage() {
  const user = await getCurrentUser();
  if (!user) redirect("/sign-in");

  return (
    <div className="flex-1 space-y-6 px-6 pt-0">
      <div className="flex flex-col gap-1">
        <p className="text-muted-foreground text-sm">Profil ayarları</p>
        <h1 className="text-2xl font-bold tracking-tight">{user.name || user.email}</h1>
        <p className="text-muted-foreground text-sm">
          Hesap bilgilerinizi ve şifrenizi buradan yönetin.
        </p>
      </div>

      <Card>
        <CardHeader>
          <CardTitle>Hesap bilgileri</CardTitle>
          <CardDescription>Kayıt tarihi ve hesap durumu</CardDescription>
        </CardHeader>
        <CardContent>
          <dl className="grid gap-4 text-sm md:grid-cols-2">
            <div>
              <dt className="text-muted-foreground text-xs uppercase">Hesap ID</dt>
              <dd className="mt-1 font-mono text-xs">{user.$id}</dd>
            </div>
            <div>
              <dt className="text-muted-foreground text-xs uppercase">Kayıt tarihi</dt>
              <dd className="mt-1">{formatDateTime(user.registration)}</dd>
            </div>
            <div>
              <dt className="text-muted-foreground text-xs uppercase">Email doğrulanmış</dt>
              <dd className="mt-1">{user.emailVerification ? "Evet" : "Hayır"}</dd>
            </div>
            <div>
              <dt className="text-muted-foreground text-xs uppercase">İki faktör (2FA)</dt>
              <dd className="mt-1">{user.mfa ? "Açık" : "Kapalı"}</dd>
            </div>
          </dl>
        </CardContent>
      </Card>

      <NameForm currentName={user.name || ""} />
      <EmailForm currentEmail={user.email} />
      <PasswordForm />
    </div>
  );
}
