# Architecture

**El Sewedy In Sawa** follows a **feature-first, layered** architecture. Each feature is self-contained, while cross-cutting concerns (networking, auth, storage, theming, responsiveness, navigation) live in shared layers.

## Layered Structure

```
lib/
├── config/
│   └── themes/          # design tokens: AppColors, AppPalette, AppTypography,
│                        # AppSpacing, AppRadius, AppShadows
├── core/
│   ├── network/         # DioHelper, interceptors
│   ├── auth/            # session, JWT utilities, guards
│   ├── storage/         # secure storage, shared_preferences
│   ├── service/         # shared services
│   ├── responsive/      # responsive engine
│   ├── navigation/      # navigation_helper, AppNavigator
│   ├── utils/           # helpers
│   ├── firebase/        # scaffolded (commented out) — see FIREBASE.md
│   └── failure/         # AppException, Failure, ExceptionMapper
├── features/
│   └── <name>/
│       ├── cubit/
│       ├── models/
│       └── presentation/
│           ├── screens/
│           └── widgets/
├── shared/
│   └── widgets/         # reusable widgets
└── l10n/ + generated/   # intl ARB + generated localizations
```

## Data Flow

The app uses a unidirectional pipeline from UI to API and back:

```
Screen → Cubit → RemoteDataSource
       → DioHelper.postProtected / postPublic
       → Dio interceptor chain → API
       → response parsed to models
       → errors → ExceptionMapper → typed AppException → localized Failure
```

Cubits expose state to screens; data sources encapsulate endpoint calls; the `DioHelper` centralizes transport. Errors are normalized: raw failures pass through an `ExceptionMapper` into typed `AppException`s, which resolve to **localized `Failure` messages** for display.

## Interceptor Chain

Requests pass through interceptors in this order:

1. **AuthInterceptor** — attaches the gateway bearer token (and, for protected routes, enforces the employee token in the body).
2. **LoggingInterceptor** — active in debug builds only.
3. **RetryCustomInterceptor** — up to **3 retries** with backoff (`dio_smart_retry`).
4. **ErrorInterceptor** — maps transport/HTTP errors into the failure pipeline.

See [API_SUMMARY.md](./API_SUMMARY.md) for the dual-token model and [SECURITY.md](./SECURITY.md) for session guarding and silent re-authentication.

## Theming

Colors are delivered as an **`AppColors` `ThemeExtension`** with complete **light and dark** token sets, accessed idiomatically via `context.colors`. Dark values are derived (no Figma dark designs were produced). Supporting tokens — `AppPalette` (raw swatches), `AppTypography`, `AppSpacing`, `AppRadius`, `AppShadows` — round out the design system.

## Responsive Engine

A custom responsive engine scales UI from a **390dp design canvas**:

- `DeviceType` = `{ mobileSmall, mobile, tablet, desktop }`.
- Extension accessors `context.w`, `context.h`, `context.sp`, `context.r` for width, height, font size, and radius scaling.
- `ResponsiveConstrained` caps content width on tablets.
- `AdaptiveText` honors the operating system's text-scaling setting for accessibility.

## Navigation

Navigation is **imperative**, implemented in `navigation_helper.dart` with a global `AppNavigator` `navigatorKey` used for **force-logout** from anywhere in the app. `app_router.dart` is intentionally empty — there is **no declarative router**.

## State Management

State is managed with **Cubits** (`bloc` / `flutter_bloc`). Representative examples:

| Cubit | Responsibility |
|-------|----------------|
| `HomeCubit` | Dashboard data (`/staff/dashboard`) |
| `AttendanceCubit` | Punch flow, shift clock, geofencing |
| `HierarchyCubit` | Org chart (`/staff/structure`) + avatars |
| `AppSettingsCubit` | Dark mode + language (persisted) |
| `PermissionsCubit` | OS location-permission management |

## Design Principles

- **Feature isolation** keeps modules independently evolvable.
- **Centralized transport** (single `DioHelper` + interceptor chain) makes auth, retry, and error handling uniform.
- **Typed failures** ensure every error surfaces as a localized, user-facing message.
- **Token-driven theming** keeps brand colors and spacing consistent across light/dark and EN/AR (RTL).
