import type { Metadata } from "next"
import { redirect } from "next/navigation"

import { requireAuthenticatedSession } from "@/lib/auth-server"
import { PortalProfileSettings } from "@/components/portal/portal-profile-settings"

export const metadata: Metadata = {
  title: "Meu perfil",
  description: "Atualize seu e-mail e defina uma senha de acesso ao portal.",
}

export default async function PortalProfilePage() {
  const session = await requireAuthenticatedSession()
  const role = (session.user.role ?? "").toLowerCase()
  const persona = (session.user.machinePersona ?? "").toLowerCase()
  const allowedRoles = new Set(["collaborator", "manager", "admin", "agent"])
  const isMachinePersonaAllowed = role === "machine" && (persona === "collaborator" || persona === "manager")
  const allowed = allowedRoles.has(role) || isMachinePersonaAllowed
  if (!allowed) {
    redirect("/portal")
  }

  return (
    <div className="space-y-6">
      <PortalProfileSettings initialEmail={session.user.email} />
    </div>
  )
}
