# AppShell

**Namespace:** `FrymasterBadgeApp`  
**Base Class:** `Shell`  
**Purpose:** MAUI Shell container responsible for dynamic tab generation, routing, theme switching, and app-level navigation.

## 🔑 Fields
| Name | Type | Access | Description |
|------|------|--------|-------------|
| `_db` | `SqlService` | `private readonly` | Database service. |
| `_printerService` | `PrinterService` | `private readonly` | Printing service. |
| `_serviceProvider` | `IServiceProvider` | `private readonly` | DI container for resolving pages. |
| `_isInitialized` | `bool` | `private` | Prevents duplicate company loading. |

## 🛠 Constructor
### `AppShell(SqlService db, PrinterService printerService, IServiceProvider serviceProvider)`
- **Access:** `public`
- **Behavior:** Registers routes for `SettingsPage`, `CompanyPage`, and `EmployeePage`.

## 📦 Methods

### `OnAppearing()`
- **Access:** `protected override async void`
- **Behavior:** Ensures `LoadCompaniesAsync()` runs exactly once when shell becomes visible.

### `LoadCompaniesAsync()`
- **Access:** `private async Task`
- **Behavior:**
  1. Fetches companies on background thread via `Task.Run`.
  2. Switches to main thread to clear `MainTabBar.Items`.
  3. If no companies: adds `"Setup"` tab routing to `CompanyPage`.
  4. Else: adds `ShellContent` tab per company routing to `EmployeePage_{ID}`.
  5. Navigates to first tab using `///{targetRoute}` (rebuilds stack).
  6. Falls back to `///InitialSetup` on DB failure.

### `OnManageCompaniesClicked(object sender, EventArgs e)`
- **Access:** `public async void`
- **Behavior:** Pushes `CompanyPage` onto navigation stack.

### `OnSettingsClicked(object sender, EventArgs e)`
- **Access:** `private async void`
- **Behavior:** Pushes `SettingsPage` onto stack.

### `OnThemeClicked(object sender, EventArgs e)`
- **Access:** `private async void`
- **Behavior:** Displays action sheet (`System`, `Light`, `Dark`). Updates `Application.Current.UserAppTheme` and persists to `Preferences.Default`.

### `OnExitClicked(object sender, EventArgs e)`
- **Access:** `private`
- **Behavior:** Calls `Application.Current?.Quit()`.

### `OnAboutClicked(object sender, EventArgs e)`
- **Access:** `private async void`
- **Behavior:** Displays alert: `"Frymaster Badge App v1.0"`.

---
**Architecture Notes:**
- Uses `///` route prefix to force full navigation stack rebuild when switching initial tabs.
- Thread-safe UI updates via `MainThread.InvokeOnMainThreadAsync`.
- Graceful degradation to setup screen on database failure.