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) => {
    const extra = c as unknown as { isAvulso?: boolean; contractedHoursPerMonth?: number | null }
    return {
      id: c.id,
      tenantId: c.tenantId,
      name: c.name,
      slug: c.slug,
      provisioningCode: c.provisioningCode ?? null,
      isAvulso: Boolean(extra.isAvulso ?? false),
      contractedHoursPerMonth: extra.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" lead="Gerencie os dados cadastrais das empresas atendidas." />
      }
    >
      <div className="mx-auto w-full max-w-7xl px-4 md:px-8 lg:px-10">
        <AdminCompaniesManager initialCompanies={companies} />
      </div>
    </AppShell>
  )
}
