'use client';
import { Stack } from '@mui/material';
import { useTranslations } from 'next-intl';
import { ProfileSummary, SurfaceCard } from '@/components';
import { SettingsPanel, SignOutRow } from '@/components/settings';
import { ROUTES } from '@/constants';
import { useAdminCapabilities } from '@/hooks';
import { useMe } from '@/services/auth';
import AdminGroupHub from '../_hub/AdminGroupHub';

/**
 * «سیستم» group root — platform configuration plus the identity/appearance/sign-out block that
 * used to live in the top bar and drawer footer. Always reachable (even for an admin role with no
 * system console permitted), because it is the only way out of the app.
 */
export default function AdminSystemPage() {
  const t = useTranslations('hub');
  const tn = useTranslations('nav');
  const ta = useTranslations('admin');
  const caps = useAdminCapabilities();
  const { data: me } = useMe();

  const primaryRoleCode = caps.roles[0];

  return (
    <AdminGroupHub
      title={tn('group_system')}
      subtitle={t('admin_system_subtitle')}
      consoles={[
        {
          title: tn('config'),
          subtitle: t('admin_config_sub'),
          icon: 'config',
          path: ROUTES.ADMIN_CONFIG,
          enabled: caps.canConfig,
        },
        {
          title: tn('holidays'),
          subtitle: t('admin_holidays_sub'),
          icon: 'calendar',
          path: ROUTES.ADMIN_HOLIDAYS,
          enabled: caps.canConfig,
        },
        {
          title: tn('audit'),
          subtitle: t('admin_audit_sub'),
          icon: 'audit',
          path: ROUTES.ADMIN_AUDIT,
          enabled: caps.canViewAudit,
        },
        {
          title: tn('partners'),
          subtitle: t('admin_partners_sub'),
          icon: 'partners',
          path: ROUTES.ADMIN_PARTNERS,
          enabled: caps.canManagePartners,
        },
        {
          title: tn('users'),
          subtitle: t('admin_users_sub'),
          icon: 'users',
          path: ROUTES.ADMIN_USERS,
          enabled: caps.canManageRoles,
        },
        {
          title: tn('roles'),
          subtitle: t('admin_roles_sub'),
          icon: 'roles',
          path: ROUTES.ADMIN_ROLES,
          enabled: caps.canManageRoles,
        },
      ]}
    >
      <Stack sx={{ gap: 2 }}>
        <SurfaceCard>
          <ProfileSummary
            displayName={me ? [me.firstName, me.lastName].filter(Boolean).join(' ').trim() || me.phone : ''}
            phone={me?.phone}
            roleLabel={primaryRoleCode ? ta(`role_${primaryRoleCode}`) : undefined}
            loading={!me}
          />
        </SurfaceCard>
        <SettingsPanel />
        <SignOutRow />
      </Stack>
    </AdminGroupHub>
  );
}
