import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core';
import { RouterLink } from '@angular/router';
import { AuthService, type CurrentUser } from 'feature-auth';
import { UserMenu, type UserMenuItem } from 'shared-ui';
import { environment } from '../../../environments/environment';

/**
 * Admin-portal header. Deliberately leaner than the user-portal
 * counterpart per ADR-0020 §"UX style is data-dense" — admins land
 * on tabular workloads, not a discovery dashboard, so we drop the
 * global search box, notification cluster, and help widget for v1.
 *
 * Two anchor points:
 *   - The "Admin" badge next to the wordmark, so an internal user
 *     instantly sees they're on the elevated surface (defense
 *     against "thought I was on portal-shell, just deleted a CMS
 *     page" muscle-memory misfires).
 *   - The auth widget: signed in → avatar with initials + Sign out,
 *     signed out → a Sign in button that 302s through the admin
 *     OIDC flow (`/api/admin/auth/login`). State driven by the
 *     shared `feature-auth` `AuthService` with `AUTH_PATH_PREFIX`
 *     overridden to `/admin/auth` (see `app.config.ts`).
 *
 * No locale switcher in v1 — admin users are a small known set
 * and the source-locale-only chrome keeps the bundle leaner.
 * Promotion when the editorial team needs FR/EN parity in the
 * admin UI itself.
 */
@Component({
  selector: 'app-admin-header',
  imports: [RouterLink, UserMenu],
  templateUrl: './header.html',
  styleUrl: './header.scss',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AdminHeader {
  private readonly auth = inject(AuthService);

  protected readonly authState = this.auth.state;

  protected readonly initials = computed(() => {
    const user = this.auth.currentUser();
    return user ? initialsFor(user) : '';
  });

  // Static menu rows. Same shape as the portal-shell menu so a user
  // who learned one finds the other familiar. The "Open Portal
  // Shell" entry is unconditional here — the user-portal is public,
  // any admin can jump back to it.
  protected readonly userMenuItems: readonly UserMenuItem[] = [
    { label: 'Profile', icon: 'user', routerLink: '/profile' },
    { label: 'Settings', icon: 'settings', disabled: true, badge: 'Soon' },
    { label: 'Open Portal Shell', icon: 'home', href: environment.shellAppUrl },
  ];

  protected readonly signedInAsLabel = 'Signed in as';
  protected readonly signOutLabel = 'Sign out';

  protected triggerAriaLabel(displayName: string): string {
    return `User menu — signed in as ${displayName}`;
  }

  protected signIn(): void {
    this.auth.login();
  }

  protected signOut(): void {
    this.auth.logout();
  }
}

function initialsFor(user: CurrentUser): string {
  const name = (user.displayName || user.username).trim();
  if (!name) {
    return '?';
  }
  const parts = name.split(/\s+/).filter(Boolean).slice(0, 2);
  const letters = parts.map((p) => p.charAt(0).toUpperCase()).join('');
  return letters || name.slice(0, 2).toUpperCase();
}
