import { AdminUsersManager } from "@/components/admin/admin-users-manager"
import { AppShell } from "@/components/app-shell"
import { SiteHeader } from "@/components/site-header"
import { ROLE_OPTIONS, normalizeRole, type RoleOption } from "@/lib/authz"
import { DEFAULT_TENANT_ID } from "@/lib/constants"
import { Prisma, prisma } from "@/lib/prisma"
import { normalizeInvite, type NormalizedInvite } from "@/server/invite-utils"
import { getServerSession } from "@/lib/auth-server"

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

function isMissingAuthTableError(error: unknown, table: string): boolean {
  if (!(error instanceof Prisma.PrismaClientKnownRequestError)) {
    return false
  }
  if (error.code !== "P2021" && error.code !== "P2023") {
    return false
  }
  const target = typeof error.meta?.table === "string" ? error.meta.table.toLowerCase() : ""
  return target.includes(table.toLowerCase())
}

async function loadUsers() {
  try {
    const users = await prisma.authUser.findMany({
      orderBy: { createdAt: "desc" },
      select: {
        id: true,
        email: true,
        name: true,
        role: true,
        tenantId: true,
        machinePersona: true,
        createdAt: true,
        updatedAt: true,
      },
    })

    if (users.length === 0) {
      return []
    }

    const domainUsers = await prisma.user.findMany({
      select: {
        email: true,
        companyId: true,
        company: {
          select: {
            id: true,
            name: true,
          },
        },
      },
    })

    const domainByEmail = new Map<string, (typeof domainUsers)[number]>(
      domainUsers.map(
        (user: (typeof domainUsers)[number]): [string, (typeof domainUsers)[number]] => [
          user.email.toLowerCase(),
          user,
        ]
      )
    )

    return users.map((user: (typeof users)[number]) => {
      const domain = domainByEmail.get(user.email.toLowerCase())
      const normalizedRole = (normalizeRole(user.role) ?? "agent") as RoleOption
      return {
        id: user.id,
        email: user.email,
        name: user.name ?? "",
        role: normalizedRole,
        tenantId: user.tenantId ?? DEFAULT_TENANT_ID,
        createdAt: user.createdAt.toISOString(),
        updatedAt: user.updatedAt?.toISOString() ?? null,
        companyId: domain?.companyId ?? null,
        companyName: domain?.company?.name ?? null,
        machinePersona: user.machinePersona ?? null,
      }
    })
  } catch (error) {
    if (isMissingAuthTableError(error, "AuthUser")) {
      console.warn("[admin] auth tables missing; returning empty user list")
      return []
    }
    throw error
  }
}

async function loadInvites(): Promise<NormalizedInvite[]> {
  try {
    const invites = await prisma.authInvite.findMany({
      orderBy: { createdAt: "desc" },
      include: {
        events: {
          orderBy: { createdAt: "asc" },
        },
      },
    })

    const now = new Date()
    const cutoff = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000)
    return invites
      .map((invite) => normalizeInvite(invite, now))
      .filter((invite: NormalizedInvite) => {
        if (invite.status !== "revoked") return true
        if (!invite.revokedAt) return true
        return new Date(invite.revokedAt) > cutoff
      })
  } catch (error) {
    if (isMissingAuthTableError(error, "AuthInvite")) {
      console.warn("[admin] auth invite tables missing; returning empty invite list")
      return []
    }
    throw error
  }
}

export default async function AdminPage() {
  const users = await loadUsers()
  const invites = await loadInvites()
  const invitesForClient = invites.map((invite) => {
    const { events, ...rest } = invite
    void events
    return rest
  })
  const session = await getServerSession()
  const viewerRole = session?.user.role ?? "agent"

  return (
    <AppShell
      header={
        <SiteHeader
          title="Administração"
          lead="Convide novos membros, ajuste papéis e organize as filas e categorias de atendimento."
        />
      }
    >
      <div className="mx-auto w-full max-w-6xl px-6 lg:px-8">
        <AdminUsersManager
          initialUsers={users}
          initialInvites={invitesForClient}
          roleOptions={ROLE_OPTIONS}
          defaultTenantId={DEFAULT_TENANT_ID}
          viewerRole={viewerRole}
          visibleTabs={["team", "invites"]}
          defaultTab="team"
        />
      </div>
    </AppShell>
  )
}
