'use client';
import { FunctionComponent, PropsWithChildren, useMemo } from 'react';
import { useTranslations } from 'next-intl';
import { Stack } from '@mui/material';
import { ProfileSummary, StatusChip } from '@/components';
import { ROUTES } from '@/constants';
import { LinkToPage } from '@/utils';
import { useMyPartnerCenter } from '@/services/partnerCenter';
import TopBarAndSideBarLayout from './TopBarAndSideBarLayout';

/**
 * Partner-center portal shell (f15) — a **separate authz scope** from the Balinyaar admin console. A
 * center admin is not a Balinyaar admin; they see only their own center's data (server-enforced
 * tenancy). Same engine as the admin shell; the TopBar identity slot shows the center's own name
 * (skeleton while resolving) **plus a compact merchant-of-record indicator** (ui-phase-11 — previously
 * only the home page's own header showed this, so it disappeared once a center admin navigated away) —
 * the page-level access-denied state (403/404) stays where it is.
 * @layout PartnerLayout
 */
const PartnerLayout: FunctionComponent<PropsWithChildren> = ({ children }) => {
  const t = useTranslations('nav');
  const tPartner = useTranslations('partner');
  const { data: center, isLoading } = useMyPartnerCenter();

  const sidebarItems: Array<LinkToPage> = useMemo(
    () => [
      { title: t('partner_home'), path: ROUTES.PARTNER, icon: 'partners' },
      { title: t('partner_nurses'), path: ROUTES.PARTNER_NURSES, icon: 'patients' },
      { title: t('partner_bookings'), path: ROUTES.PARTNER_BOOKINGS, icon: 'bookings' },
      { title: t('partner_settlement'), path: ROUTES.PARTNER_SETTLEMENT, icon: 'earnings' },
    ],
    [t]
  );

  return (
    <TopBarAndSideBarLayout
      sidebarItems={sidebarItems}
      identity={
        <Stack direction="row" sx={{ gap: 1, alignItems: 'center', minWidth: 0 }}>
          <ProfileSummary compact displayName={center?.name ?? ''} loading={isLoading} />
          {!isLoading && center ? (
            <StatusChip
              status={center.isMerchantOfRecord ? 'active' : 'neutral'}
              label={center.isMerchantOfRecord ? tPartner('is_mor_yes') : tPartner('is_mor_no')}
              sx={{ height: 20, fontSize: '0.6875rem', display: { xs: 'none', sm: 'inline-flex' } }}
            />
          ) : null}
        </Stack>
      }
    >
      {children}
    </TopBarAndSideBarLayout>
  );
};

export default PartnerLayout;
