# selmetwa/parallel-arabic architecture

> Architecture diagram of the GitHub repository selmetwa/parallel-arabic, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/selmetwa/parallel-arabic
- Repository: https://github.com/selmetwa/parallel-arabic

## Overview

##### Project Type & Purpose
- This is a full-stack, server-rendered web application built with SvelteKit.
- Purpose: A comprehensive Arabic language-learning platform offering interactive lessons (stories, vocabulary, grammar, speaking, writing), powered by AI (OpenAI, TTS/STT), with subscription payments (Stripe) and user management (Supabase).

##### File Structure Highlights
- src/routes: SvelteKit pages and API endpoints (server.ts handlers)
- src/lib: Shared components (Svelte UI), helpers, services (stripe.service.ts), utils (api-client, transformers), and supabaseClient
- static: Pre-recorded audio, images, uploads—served as static assets via CDN
- Dockerfile, fly.toml: Containerized for deployment (Fly.io)
- playwright.config.ts, tests/: End-to-end testing
- package.json, tsconfig.json, eslint/prettier: Build and lint configuration

##### Key README Insights
- Multi-dialect support
- Core features: Story comprehension, speaking practice (STT similarity scoring), sentence writing, grammar modules, vocabulary drills, alphabet mastery
- AI-powered content (OpenAI for sentence/story generation, definitions)
- Stripe for subscription/payment
- Supabase for auth, database, storage

##### Main Components & Interactions
a. Frontend (Browser)
- UI: Svelte/SvelteKit pages + components
- Virtual keyboard, drawing canvas, audio player, chat widget

b. Application Server (SvelteKit)
- Route handlers in src/routes/api/** invoke business logic
- Server hooks (hooks.server.ts) for auth guard/sessions
- Integrates supabase-auth-helpers, stripe.service

c. Database & Auth (Supabase)
- PostgreSQL for user profiles, saved words, progress tracking
- Supabase Auth for sign-up/login, OAuth (Google)

d. Storage & Static Assets
- Supabase Storage or Fly.io volumes for dynamic audio/file uploads
- Pre-recorded and generated audio under static/ and data/

e. External AI & Media Services
- OpenAI (chat-GPT) for story/sentence generation (via dialect-api.ts)
- Text-to-Speech & Speech-to-Text endpoints (local wrappers or 3rd-party)
- YouTube Transcript API for video materials

f. Payment Gateway (Stripe)
- stripe.service.ts invoked by /api/stripe-webhook, /pricing/checkout endpoints
- Manages subscription tiers and webhooks

##### Architectural Patterns & Design Principles
- Modular component library: src/lib/components for reusable UI
- Route-based backend: each API corresponds to a single responsibility
- Service layer: stripe.service.ts, audio-generation.ts, email.ts isolate external integrations
- State management: Svelte stores (src/lib/store, src/lib/stores) for client state sync with Supabase
- CI/CD: GitHub Actions + Fly.io deployment

- User’s Browser (Frontend)
- SvelteKit Server (Routes & API Handlers)
- Supabase (Auth, Database, Storage)
- Stripe (Payment)
- AI Services (OpenAI, TTS, STT, YouTube API)
- CDN/Static Asset Store (static/)

- Browser → SvelteKit: page requests, API calls (REST/JSON)
- SvelteKit → Supabase Auth/DB/Storage: JWT auth, CRUD operations, file uploads/downloads
- SvelteKit → Stripe: create checkout session, handle webhooks
- SvelteKit → AI Services: HTTP calls for story generation, TTS/STT, definitions
- Browser ↔ Supabase realtime (if applicable) for live updates

- Green for managed services (Supabase)
- Yellow for external APIs (OpenAI, Stripe)

- HTTPS/REST, WebSockets (if used), JWT/OAuth tokens

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend" 
        Browser["User's Browser"]:::frontend
        UI["UI Components"]:::component
        Keyboard["Virtual Keyboard"]:::component
        Canvas["Drawing Canvas"]:::component
        AudioPlayer["Audio.svelte & AudioButton.svelte"]:::component
        ChatWidget["Chat Widget"]:::component
    end

    subgraph "Backend" 
        Server["SvelteKit Server"]:::backend
        Hooks["Auth & Session Hooks"]:::backend
        APIRoutes["API Route Handlers"]:::backend
        SupabaseClient["Supabase Client Init"]:::backend
        StripeService["Stripe Service Layer"]:::backend
        StripeWebhook["Stripe Webhook Endpoint"]:::backend
        CheckoutLogic["Checkout Page Logic"]:::backend
    end

    subgraph "Services"
        Supabase["Supabase (Auth, DB, Storage)"]:::supabase
        DB[(PostgreSQL Database)]:::db
        OpenAIClient["dialect-api.ts"]:::external
        OpenAIRoute["OpenAI Route Endpoint"]:::external
        StoryHelper["ask-chat-gpt.ts"]:::component
        TTSService["audio-generation.ts"]:::external
        TTSRoute["Text-to-Speech API"]:::external
        STTRoute["Speech-to-Text & Transcribe APIs"]:::external
        YTRoute["YouTube Transcript API"]:::external
        Stripe["Stripe"]:::external
        CDN["CDN / Static Assets"]:::cdn
        CI["CI/CD & Deployment"]:::deployment
    end

    Browser -->|"HTTPS/REST"| Server
    Browser --> UI
    UI --> Keyboard
    UI --> Canvas
    UI --> AudioPlayer
    UI --> ChatWidget

    Server --> Hooks
    Server --> APIRoutes
    APIRoutes --> SupabaseClient
    APIRoutes --> StripeService
    APIRoutes --> OpenAIRoute
    APIRoutes --> TTSRoute
    APIRoutes --> STTRoute
    APIRoutes --> YTRoute

    APIRoutes -->|"Webhook"| StripeWebhook
    APIRoutes -->|"Checkout"| CheckoutLogic

    SupabaseClient --> Supabase
    Hooks --> Supabase
    StripeService --> Stripe
    OpenAIRoute --> OpenAIClient
    OpenAIRoute --> StoryHelper
    TTSRoute --> TTSService
    CDN -->|"Static Asset Serve"| Browser

    Supabase --> DB

    CI --> Server

    classDef frontend fill:#cce5ff,stroke:#004085,color:#004085
    classDef component fill:#e2e3e5,stroke:#6c757d,color:#343a40
    classDef backend fill:#b8daff,stroke:#004085,color:#004085
    classDef supabase fill:#d4edda,stroke:#155724,color:#155724
    classDef external fill:#fff3cd,stroke:#856404,color:#856404
    classDef cdn fill:#e2e3e5,stroke:#6c757d,color:#343a40
    classDef db fill:#d6d8db,stroke:#1b1e21,color:#1b1e21
    classDef deployment fill:#f8d7da,stroke:#721c24,color:#721c24

    click UI "https://github.com/selmetwa/parallel-arabic/tree/master/src/lib/components"
    click Keyboard "https://github.com/selmetwa/parallel-arabic/blob/master/src/lib/components/KeyboardDocumentation.svelte"
    click Canvas "https://github.com/selmetwa/parallel-arabic/blob/master/src/lib/components/Canvas.svelte"
    click AudioPlayer "https://github.com/selmetwa/parallel-arabic/blob/master/src/lib/components/Audio.svelte"
    click AudioPlayer "https://github.com/selmetwa/parallel-arabic/blob/master/src/lib/components/AudioButton.svelte"
    click ChatWidget "https://github.com/selmetwa/parallel-arabic/blob/master/src/lib/components/ChatWidget.svelte"
    click Hooks "https://github.com/selmetwa/parallel-arabic/blob/master/src/hooks.server.ts"
    click APIRoutes "https://github.com/selmetwa/parallel-arabic/tree/master/src/routes/api/"
    click SupabaseClient "https://github.com/selmetwa/parallel-arabic/blob/master/src/lib/supabaseClient.ts"
    click StripeService "https://github.com/selmetwa/parallel-arabic/blob/master/src/lib/services/stripe.service.ts"
    click StripeWebhook "https://github.com/selmetwa/parallel-arabic/blob/master/src/routes/api/stripe-webhook/+server.ts"
    click CheckoutLogic "https://github.com/selmetwa/parallel-arabic/blob/master/src/routes/pricing/checkout/+page.server.ts"
    click OpenAIClient "https://github.com/selmetwa/parallel-arabic/blob/master/src/lib/utils/dialect-api.ts"
    click OpenAIRoute "https://github.com/selmetwa/parallel-arabic/blob/master/src/routes/api/open-ai/+server.ts"
    click StoryHelper "https://github.com/selmetwa/parallel-arabic/blob/master/src/lib/components/dialect-shared/story/helpers/ask-chat-gpt.ts"
    click TTSService "https://github.com/selmetwa/parallel-arabic/blob/master/src/lib/server/audio-generation.ts"
    click TTSRoute "https://github.com/selmetwa/parallel-arabic/blob/master/src/routes/api/text-to-speech/+server.ts"
    click STTRoute "https://github.com/selmetwa/parallel-arabic/blob/master/src/routes/api/speech-to-text/+server.ts"
    click STTRoute "https://github.com/selmetwa/parallel-arabic/blob/master/src/routes/api/audio-transcribe/+server.ts"
    click YTRoute "https://github.com/selmetwa/parallel-arabic/blob/master/src/routes/api/youtube-transcript/+server.ts"
    click CI "https://github.com/selmetwa/parallel-arabic/tree/master/Dockerfile"
```
