# mutaz-alqaimary/bayan architecture

> Architecture diagram of the GitHub repository mutaz-alqaimary/bayan, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mutaz-alqaimary/bayan
- Repository: https://github.com/mutaz-alqaimary/bayan

## Overview

1. **System shape**
   - Bayan is a **Next.js 16 App Router** application for Arabic literacy and reading-fluency tracking.
   - The repo is organized around **locale-prefixed routes** and **feature modules**, with Supabase as the persistent backend.

2. **Route topology**
   - `app/[locale]/(auth)` contains the public authentication flow: login, register, forgot password, and reset password.
   - `app/[locale]/(app)` contains the authenticated product areas: dashboard, students, teachers, passages, vocabulary, reading sessions, analytics, reports, and settings.
   - The root locale shell lives in `app/[locale]/layout.tsx`, with localized not-found/error boundaries and route-level loading/error states per section.

3. **Localization and RTL/LTR boundary**
   - The app is built for **Arabic-first** usage with both `ar` and `en`.
   - Locale negotiation and redirects are handled at the edge/proxy layer in `proxy.ts`, using `next-intl`.
   - The i18n boundary is enforced through `i18n/navigation.ts`, `i18n/request.ts`, and the translation bundles in `messages/`.
   - This is a core architectural boundary because it affects routing, metadata, text direction, and UI composition.

4. **Authenticated app shell**
   - Shared logged-in chrome is assembled from `components/layout/` and wrapped by app-level providers in `components/providers/`.
   - The shell includes sidebar/topbar/mobile navigation, theme toggle, locale switcher, user menu, and skip-link/accessibility affordances.
   - The shared shell is what gives the product its persistent navigation and responsive behavior across all authenticated sections.

5. **Feature-module architecture**
   - Business functionality is split into domain folders under `features/`: `auth`, `dashboard`, `reading`, `students`, `teachers`, `analytics`, `reporting`, and `settings`.
   - Each feature typically owns its own:
     - route-facing page component(s),
     - UI components,
     - data/query helpers,
     - schemas/types,
     - server actions where needed.
   - This is the main modular boundary in the repo: routes are thin, features hold the business logic and presentation for each domain.

6. **Data flow to and from Supabase**
   - Supabase is the system of record, configured through `lib/supabase/` and the SQL contract in `supabase/schema.sql`.
   - The codebase distinguishes between:
     - **browser/client access** via the public Supabase client,
     - **server-side access** via server helpers,
     - **privileged admin access** via the service-role client.
   - Feature-level `actions.ts` and `queries.ts` files are the main path for reading and mutating data, keeping persistence concerns outside the route layer.

7. **Auth and session handling**
   - Authentication is implemented as a dedicated feature plus a callback route at `app/api/auth/callback/route.ts`.
   - `features/auth/` contains forms, guards, schema validation, role logic, and auth-related queries/actions.
   - This subsystem bridges the browser login forms, Supabase auth/session state, and the protected app routes.

8. **Student, reading, and instructional workflows**
   - `features/students/` manages student records plus identity alignment and import/export flows.
   - `features/reading/` manages passages and vocabulary content, while `features/reading/sessions/` handles reading-session execution, review, fluency computation, and session history.
   - These modules form the operational core of the platform: content is prepared, sessions are run, then outcomes are stored and later reused for analytics and reporting.

9. **Analytics and reporting pipeline**
   - `features/analytics/` aggregates session and reading data into KPIs, cohort trends, student summaries, and charts.
   - `features/reporting/` turns those same data models into printable/report documents for student or cohort views.
   - The architecture here is read-optimized: query/transform code sits apart from visual components so metrics can be reused across dashboard, analytics, and reporting surfaces.

10. **Dashboard as a role-sensitive overview layer**
   - `features/dashboard/` provides different summaries for admin, teacher, and student roles.
   - The dashboard is not a separate backend; it is a presentation layer over the same domain data, with role-aware variants and reusable stat tiles, sparklines, and activity summaries.

11. **Shared UI and design system**
   - `components/ui/` contains the reusable UI primitives used across all features, alongside toast, table, dialog, drawer, select, tabs, and form controls.
   - Styling is Tailwind CSS v4 with design tokens in `app/globals.css`, plus theme management in `lib/theme.ts` and `components/theme-init-script.tsx`.
   - This layer is the visual foundation beneath the feature modules and helps keep the app consistent across locales and themes.

12. **Cross-cutting libraries and runtime concerns**
   - `lib/` holds reusable infrastructure: formatting, route constants, validation helpers, fonts, motion, errors, site URL handling, and avatar/collation utilities.
   - `instrumentation.ts` and test scaffolding under `test/` support runtime observability and verification.
   - The repo also includes SEO and platform surfaces such as `app/robots.ts` and `app/sitemap.ts`.

13. **Testing and quality boundaries**
   - Tests are colocated with features for domain logic, schema validation, and UI behavior, with additional integration and smoke coverage under `test/`.
   - This suggests a design where architecture is validated at the module boundary: parsing, aggregation, auth roles, reporting metadata, import classification, and fluency calculations are all treated as distinct units.

14. **Documentation and governance**
   - The `docs/phases/` and `docs/project/` folders track the intended architecture and rollout history.
   - The `.claude/rules/` and supporting docs indicate the repository has strong conventions around schema, RTL/i18n, accessibility, and design-system consistency.
   - For a diagram, treat these as governance artifacts around the codebase rather than runtime subsystems.

Main entry points: [`lib/supabase/client.ts`](https://github.com/mutaz-alqaimary/bayan/blob/main/lib/supabase/client.ts) (Supabase stack), [`components/ui/button.tsx`](https://github.com/mutaz-alqaimary/bayan/blob/main/components/ui/button.tsx) (UI system), [`app/[locale]/layout.tsx`](https://github.com/mutaz-alqaimary/bayan/blob/main/app/[locale]/layout.tsx) (Locale shell), [`features/auth/actions.ts`](https://github.com/mutaz-alqaimary/bayan/blob/main/features/auth/actions.ts) (Auth feature)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_app["App routes"]
  node_node_locale_shell["Locale shell<br/>nextjs app layout<br/>[layout.tsx]"]
  node_node_auth_routes["Auth routes<br/>[page.tsx]"]
  node_node_app_routes["Product routes<br/>nextjs app routes<br/>[page.tsx]"]
  node_node_api_callback["Auth callback<br/>api route<br/>[route.ts]"]
end

subgraph group_group_features["Feature modules"]
  node_node_auth_feature["Auth feature<br/>feature auth<br/>[actions.ts]"]
  node_node_dashboard_feature["Dashboard<br/>feature overview"]
  node_node_students_feature["Students<br/>[actions.ts]"]
  node_node_teachers_feature["Teachers<br/>[actions.ts]"]
  node_node_reading_feature["Reading content<br/>feature reading<br/>[actions.ts]"]
  node_node_sessions_feature["Reading sessions<br/>feature sessions<br/>[actions.ts]"]
  node_node_analytics_feature["Analytics<br/>[aggregate.ts]"]
  node_node_reporting_feature["Reporting<br/>[queries.ts]"]
  node_node_settings_feature["Settings<br/>[actions.ts]"]
end

subgraph group_group_infra["Infrastructure"]
  node_node_proxy_i18n{{"Locale proxy<br/>edge i18n<br/>[proxy.ts]"}}
  node_node_i18n{{"I18n core<br/>routing/request<br/>[navigation.ts]"}}
  node_node_app_shell["App shell<br/>layout chrome<br/>[app-shell.tsx]"]
  node_node_providers["Providers<br/>[app-providers.tsx]"]
  node_node_ui_system["UI system<br/>design system<br/>[button.tsx]"]
  node_node_supabase[("Supabase stack<br/>backend clients<br/>[client.ts]")]
  node_node_shared_lib{{"Shared libs<br/>utilities<br/>[routes.ts]"}}
end

node_node_proxy_i18n -->|"negotiates locale"| node_node_i18n
node_node_i18n -->|"feeds routing"| node_node_locale_shell
node_node_locale_shell -->|"wraps"| node_node_auth_routes
node_node_locale_shell -->|"wraps"| node_node_app_routes
node_node_locale_shell -.->|"supports auth"| node_node_api_callback
node_node_app_routes -->|"renders chrome"| node_node_app_shell
node_node_app_shell -->|"uses"| node_node_providers
node_node_app_shell -->|"composes"| node_node_ui_system
node_node_auth_routes -->|"uses"| node_node_auth_feature
node_node_api_callback -->|"completes session"| node_node_supabase
node_node_auth_feature -->|"auth data"| node_node_supabase
node_node_auth_feature -->|"validates and guards"| node_node_shared_lib
node_node_dashboard_feature -->|"reads metrics"| node_node_supabase
node_node_students_feature -->|"manages roster"| node_node_supabase
node_node_reading_feature -->|"manages content"| node_node_supabase
node_node_sessions_feature -->|"stores sessions"| node_node_supabase
node_node_analytics_feature -->|"aggregates data"| node_node_supabase
node_node_reporting_feature -->|"builds reports"| node_node_supabase
node_node_settings_feature -->|"updates profile"| node_node_supabase
node_node_dashboard_feature -->|"charts and cards"| node_node_ui_system
node_node_analytics_feature -->|"visualizes"| node_node_ui_system
node_node_reporting_feature -->|"prints and layouts"| node_node_ui_system
node_node_students_feature -->|"tables and dialogs"| node_node_ui_system

click node_node_locale_shell "https://github.com/mutaz-alqaimary/bayan/blob/main/app/[locale]/layout.tsx"
click node_node_auth_routes "https://github.com/mutaz-alqaimary/bayan/blob/main/app/[locale]/(auth)/login/page.tsx"
click node_node_app_routes "https://github.com/mutaz-alqaimary/bayan/blob/main/app/[locale]/(app)/dashboard/page.tsx"
click node_node_api_callback "https://github.com/mutaz-alqaimary/bayan/blob/main/app/api/auth/callback/route.ts"
click node_node_proxy_i18n "https://github.com/mutaz-alqaimary/bayan/blob/main/proxy.ts"
click node_node_i18n "https://github.com/mutaz-alqaimary/bayan/blob/main/i18n/navigation.ts"
click node_node_app_shell "https://github.com/mutaz-alqaimary/bayan/blob/main/components/layout/app-shell.tsx"
click node_node_providers "https://github.com/mutaz-alqaimary/bayan/blob/main/components/providers/app-providers.tsx"
click node_node_ui_system "https://github.com/mutaz-alqaimary/bayan/blob/main/components/ui/button.tsx"
click node_node_supabase "https://github.com/mutaz-alqaimary/bayan/blob/main/lib/supabase/client.ts"
click node_node_shared_lib "https://github.com/mutaz-alqaimary/bayan/blob/main/lib/routes.ts"
click node_node_auth_feature "https://github.com/mutaz-alqaimary/bayan/blob/main/features/auth/actions.ts"
click node_node_dashboard_feature "https://github.com/mutaz-alqaimary/bayan/blob/main/features/dashboard/components/admin-dashboard.tsx"
click node_node_students_feature "https://github.com/mutaz-alqaimary/bayan/blob/main/features/students/actions.ts"
click node_node_teachers_feature "https://github.com/mutaz-alqaimary/bayan/blob/main/features/teachers/actions.ts"
click node_node_reading_feature "https://github.com/mutaz-alqaimary/bayan/blob/main/features/reading/actions.ts"
click node_node_sessions_feature "https://github.com/mutaz-alqaimary/bayan/blob/main/features/reading/sessions/actions.ts"
click node_node_analytics_feature "https://github.com/mutaz-alqaimary/bayan/blob/main/features/analytics/aggregate.ts"
click node_node_reporting_feature "https://github.com/mutaz-alqaimary/bayan/blob/main/features/reporting/queries.ts"
click node_node_settings_feature "https://github.com/mutaz-alqaimary/bayan/blob/main/features/settings/actions.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_locale_shell,node_node_auth_routes,node_node_app_routes,node_node_api_callback toneBlue
class node_node_auth_feature,node_node_dashboard_feature,node_node_students_feature,node_node_teachers_feature,node_node_reading_feature,node_node_sessions_feature,node_node_analytics_feature,node_node_reporting_feature,node_node_settings_feature toneAmber
class node_node_proxy_i18n,node_node_i18n,node_node_app_shell,node_node_providers,node_node_ui_system,node_node_supabase,node_node_shared_lib toneMint
```

## Components

### App routes

Locale-scoped App Router surfaces for public auth and authenticated product areas.

- [Locale shell](https://github.com/mutaz-alqaimary/bayan/blob/main/app/[locale]/layout.tsx): nextjs app layout · Root locale-aware App Router shell, with per-locale not-found and route boundaries. · `app/[locale]/layout.tsx`
- [Auth routes](https://github.com/mutaz-alqaimary/bayan/blob/main/app/[locale]/(auth)/login/page.tsx): nextjs auth routes · Public login, register, forgot-password, and reset-password routes. · `app/[locale]/(auth)/login/page.tsx`
- [Product routes](https://github.com/mutaz-alqaimary/bayan/blob/main/app/[locale]/(app)/dashboard/page.tsx): nextjs app routes · Authenticated sections for dashboard, students, teachers, passages, vocabulary, sessions, analytics, reports, and settings. · `app/[locale]/(app)/dashboard/page.tsx`
- [Auth callback](https://github.com/mutaz-alqaimary/bayan/blob/main/app/api/auth/callback/route.ts): api route · Supabase auth callback endpoint that completes session handling. · `app/api/auth/callback/route.ts`

### Feature modules

Domain modules that hold business logic, UI, queries, actions, and schemas.

- [Auth feature](https://github.com/mutaz-alqaimary/bayan/blob/main/features/auth/actions.ts): feature auth · Forms, guards, roles, schemas, queries, and actions for authentication and session state. · `features/auth/actions.ts`
- [Dashboard](https://github.com/mutaz-alqaimary/bayan/blob/main/features/dashboard/components/admin-dashboard.tsx): feature overview · Role-sensitive dashboard summaries and shared KPI/insight components. · `features/dashboard/components/admin-dashboard.tsx`
- [Students](https://github.com/mutaz-alqaimary/bayan/blob/main/features/students/actions.ts): feature students · Student records, roster management, identity alignment, and import/export workflows. · `features/students/actions.ts`
- [Teachers](https://github.com/mutaz-alqaimary/bayan/blob/main/features/teachers/actions.ts): feature teachers · Teacher management with promotion and demotion flows. · `features/teachers/actions.ts`
- [Reading content](https://github.com/mutaz-alqaimary/bayan/blob/main/features/reading/actions.ts): feature reading · Passages and vocabulary content management for instructional workflows. · `features/reading/actions.ts`
- [Reading sessions](https://github.com/mutaz-alqaimary/bayan/blob/main/features/reading/sessions/actions.ts): feature sessions · Session execution, review, fluency computation, and history. · `features/reading/sessions/actions.ts`
- [Analytics](https://github.com/mutaz-alqaimary/bayan/blob/main/features/analytics/aggregate.ts): feature analytics · Aggregation and visualization of cohort, student, and reading metrics. · `features/analytics/aggregate.ts`
- [Reporting](https://github.com/mutaz-alqaimary/bayan/blob/main/features/reporting/queries.ts): feature reporting · Printable report generation over shared student and cohort data. · `features/reporting/queries.ts`
- [Settings](https://github.com/mutaz-alqaimary/bayan/blob/main/features/settings/actions.ts): feature settings · User profile and app settings forms, actions, and schemas. · `features/settings/actions.ts`

### Infrastructure

Shared runtime services for localization, Supabase, UI shell, and cross-cutting utilities.

- [Locale proxy](https://github.com/mutaz-alqaimary/bayan/blob/main/proxy.ts): edge i18n · Locale negotiation and redirect boundary using next-intl. · `proxy.ts`
- [I18n core](https://github.com/mutaz-alqaimary/bayan/blob/main/i18n/navigation.ts): routing/request · Localized navigation and request resolution plus translation bundles. · `i18n/navigation.ts`
- [App shell](https://github.com/mutaz-alqaimary/bayan/blob/main/components/layout/app-shell.tsx): layout chrome · Shared authenticated chrome and responsive navigation wrapper. · `components/layout/app-shell.tsx`
- [Providers](https://github.com/mutaz-alqaimary/bayan/blob/main/components/providers/app-providers.tsx): client providers · Theme, motion, and app-wide context providers. · `components/providers/app-providers.tsx`
- [UI system](https://github.com/mutaz-alqaimary/bayan/blob/main/components/ui/button.tsx): design system · Reusable UI primitives and toasts used across features. · `components/ui/button.tsx`
- [Supabase stack](https://github.com/mutaz-alqaimary/bayan/blob/main/lib/supabase/client.ts): backend clients · Client, server, admin, and proxy helpers for Supabase access plus schema contract. · `lib/supabase/client.ts`
- [Shared libs](https://github.com/mutaz-alqaimary/bayan/blob/main/lib/routes.ts): utilities · Cross-cutting utilities for formatting, routes, validation, errors, theme, fonts, and observability. · `lib/routes.ts`

## Connections

- Locale proxy → I18n core: negotiates locale · Edge layer redirects and resolves locale-prefixed routes.
- I18n core → Locale shell: feeds routing · Localized routing and request config drive the app shell.
- Locale shell → Auth routes: wraps · Locale layout contains the public auth section.
- Locale shell → Product routes: wraps · Locale layout contains the authenticated product area.
- Locale shell → Auth callback: supports auth · Locale/session flow is completed through the auth callback route.
- Product routes → App shell: renders chrome · Authenticated routes are displayed inside the shared shell.
- App shell → Providers: uses · Shell is composed under app-wide providers.
- App shell → UI system: composes · Shell uses shared UI primitives for navigation and controls.
- Auth routes → Auth feature: uses · Auth pages are thin route surfaces over the auth module.
- Auth callback → Supabase stack: completes session · Callback exchanges auth state through Supabase helpers.
- Auth feature → Supabase stack: auth data · Authentication queries and actions use Supabase clients.
- Auth feature → Shared libs: validates and guards · Roles, schema messages, and guards depend on shared utilities.
- Dashboard → Supabase stack: reads metrics · Dashboard summaries are built from domain data queried through Supabase.
- Students → Supabase stack: manages roster · Student CRUD, identity, and import/export flows persist through Supabase.
- Reading content → Supabase stack: manages content · Passages and vocabulary are stored and queried via Supabase.
- Reading sessions → Supabase stack: stores sessions · Reading sessions and fluency outcomes are persisted in Supabase.
- Analytics → Supabase stack: aggregates data · Analytics queries pull from session and reading records.
- Reporting → Supabase stack: builds reports · Reporting reads the same operational data for printable outputs.
- Settings → Supabase stack: updates profile · Settings actions and queries persist user configuration through Supabase.
- Dashboard → UI system: charts and cards · Dashboard views are built from shared UI primitives.
- Analytics → UI system: visualizes · Analytics components rely on shared charts, tabs, tables, and feedback states.
- Reporting → UI system: prints and layouts · Reporting uses shared controls and document layout primitives.
- Students → UI system: tables and dialogs · Student workflows are implemented with shared tables, drawers, and forms.
