import Link from "next/link";
import type { ReactNode } from "react";
import { SignOutButton } from "@/components/auth/signout-button";
import { Sidebar } from "@/components/dashboard/sidebar";
import { requireCurrentUser } from "@/lib/session";

export default async function DashboardLayout({
  children
}: {
  children: ReactNode;
}) {
  const user = await requireCurrentUser();

  return (
    <div className="dashboard-shell flex min-h-screen flex-col">
      <header className="mb-7 flex flex-wrap items-start justify-between gap-4 border-b border-border/80 pb-5">
        <div className="space-y-1">
          <Link
            href="/"
            className="inline-flex items-center rounded-md border border-border/90 bg-panel/70 px-3 py-2 text-base font-bold uppercase tracking-[0.24em] text-text transition-colors hover:border-accent/60 hover:text-accent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/70 focus-visible:ring-offset-2 focus-visible:ring-offset-bg"
            aria-label="Go to homepage"
            title="Go to homepage"
          >
            PAYME
          </Link>
          <p className="text-sm text-muted">{user.email}</p>
        </div>

        <SignOutButton />
      </header>

      <div className="flex flex-col gap-7 md:flex-row">
        <Sidebar username={user.profile?.username} />
        <main className="min-w-0 flex-1">{children}</main>
      </div>
    </div>
  );
}
