import { AppShell } from "@/components/app-shell"
import { SiteHeader } from "@/components/site-header"
import { prisma } from "@/lib/prisma"
import { AdminCompaniesManager } from "@/components/admin/companies/admin-companies-manager"

export const runtime = "nodejs"
export const dynamic = "force-dynamic"

export default async function AdminCompaniesPage() {
  const companiesRaw = await prisma.company.findMany({ orderBy: { name: "asc" } })
  const companies = companiesRaw.map((c) => ({
    id: c.id,
    tenantId: c.tenantId,
    name: c.name,
    slug: c.slug,
    isAvulso: Boolean(c.isAvulso ?? false),
    contractedHoursPerMonth: c.contractedHoursPerMonth ?? null,
    cnpj: c.cnpj ?? null,
    domain: c.domain ?? null,
    phone: c.phone ?? null,
    description: c.description ?? null,
    address: c.address ?? null,
  }))
  return (
    <AppShell
      header={
        <SiteHeader
          title="Empresas & Clientes"
          lead="Gerencie os dados das empresas e controle o faturamento de clientes avulsos."
        />
      }
    >
      <div className="mx-auto w-full max-w-6xl px-6 lg:px-8">
        <AdminCompaniesManager initialCompanies={companies} />
      </div>
    </AppShell>
  )
}
