/**
 * @module cortex/pages/team/TeamPage
 * @audience installer
 * @layer frontend-page
 * @stability stable
 *
 * Cortex team management page. Visible to support_admin and system_admin.
 * Invites are scoped to the spine-system account with selectable cortex roles.
 * List is filtered to cortex roles only (support, support_admin).
 *
 * Route: /cortex/team
 *
 * @seeAlso src/components/shared/InviteManager.tsx
 */

import React from 'react'
import { InviteManager } from '@core/components/shared/InviteManager'
import { useAuth } from '@core/contexts/AuthContext'

const CORTEX_ROLES = [
  { slug: 'support', name: 'Support' },
  { slug: 'support_admin', name: 'Support Admin' },
]

const CORTEX_ROLE_SLUGS = CORTEX_ROLES.map(r => r.slug).join(',')

export function TeamPage() {
  const { user } = useAuth()

  const isAllowed =
    user?.is_system_admin ||
    user?.roles?.some((r: string) => r === 'support_admin' || r === 'system_admin')

  if (!isAllowed) {
    return (
      <div className="p-6">
        <p className="text-sm text-slate-500">You don't have permission to manage team members.</p>
      </div>
    )
  }

  return (
    <div className="p-6 space-y-6">
      <div>
        <h1 className="text-2xl font-semibold text-slate-900">Team</h1>
        <p className="mt-1 text-sm text-slate-500">
          Invite support staff and manage access to Cortex.
        </p>
      </div>

      <InviteManager
        scope="account"
        accountId={user?.account_id}
        allowedRoles={CORTEX_ROLES}
        roleSlugsFilter={CORTEX_ROLE_SLUGS}
      />
    </div>
  )
}
