import Link from 'next/link';
import type { Metadata } from 'next';
import { requireUser } from '@/lib/auth/current-user';
import { db } from '@/db';
import { tenants } from '@/db/schema';
import { eq } from 'drizzle-orm';
import AppNav from '@/components/AppNav';

const PANEL_LINKS = [
  { href: '/panel', label: 'Leads', icon: 'leads' },
  { href: '/panel/precios', label: 'Precios', icon: 'precios' },
  { href: '/panel/empresa', label: 'Empresa', icon: 'empresa' },
] as const;

export const metadata: Metadata = {
  title: 'Panel · Reformix',
  description: 'Panel de leads del reformista',
};

export default async function PanelLayout({ children }: { children: React.ReactNode }) {
  const user = await requireUser();
  const [tenant] = user.tenantId
    ? await db.select().from(tenants).where(eq(tenants.id, user.tenantId)).limit(1)
    : [];
  const nombreEmpresa = tenant?.nombreEmpresa ?? 'Reformix';

  return (
    <div className="min-h-screen bg-gray-50">
      <header className="sticky top-0 z-20 bg-white border-b border-gray-200">
        <div className="relative max-w-6xl mx-auto px-6 h-16 flex items-center justify-between">
          <Link href="/panel" className="flex items-center gap-2 min-w-0">
            <span className="inline-flex shrink-0 items-center justify-center w-8 h-8 rounded-lg bg-black text-white font-black italic text-lg leading-none">
              R
            </span>
            <span className="font-extrabold tracking-tight text-black">Reformix</span>
            <span className="hidden sm:inline text-gray-300">/</span>
            <span className="hidden sm:inline text-sm font-medium text-gray-600 truncate">
              {nombreEmpresa}
            </span>
          </Link>
          <AppNav links={PANEL_LINKS} />
        </div>
      </header>
      <main className="max-w-6xl mx-auto px-6 py-8 pb-24 sm:pb-8">{children}</main>
    </div>
  );
}
