# samuraigpt/ai-hair-style-simulator architecture

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

- Interactive diagram: https://gitdiagram.com/samuraigpt/ai-hair-style-simulator
- Repository: https://github.com/samuraigpt/ai-hair-style-simulator

## Overview

This is a Next.js virtual hairstyle makeover SaaS. A signed-in user uploads a portrait, chooses style and color parameters, and submits a generation; results are saved for the personal gallery. Separate branches cover Google/API-key authentication and one-time Stripe credit purchases. The README documents these capabilities; implementation excerpts show generation, credit, and persistence calls, while unsampled UI and webhook wiring is represented only where documented.

Main entry points: [`prisma/schema.prisma`](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/prisma/schema.prisma) (PostgreSQL data), [`src/app/api/generation/route.js`](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/generation/route.js) (Hairstyle generation), [`src/lib/services/billing.js`](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/lib/services/billing.js) (Billing service), [`src/app/page.js`](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/page.js) (Makeover studio)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_makeover["Makeover workflow"]
  node_studio["Makeover studio<br/>[page.js]"]
  node_image_upload["Portrait upload<br/>[route.js]"]
  node_generation["Hairstyle generation<br/>[route.js]"]
  node_muapi_callback["Generation callback<br/>[route.js]"]
end

subgraph group_accounts["Accounts and credits"]
  node_login["Login page<br/>[page.js]"]
  node_auth["Authentication<br/>[auth.js]"]
  node_auth_route["Auth endpoint<br/>[route.js]"]
  node_navbar["Account navigation<br/>[Navbar.js]"]
  node_apikey["API key management<br/>[route.js]"]
  node_user_service["Credit operations<br/>[user.js]"]
end

subgraph group_showroom["Showroom and storage"]
  node_gallery["Personal gallery<br/>[page.js]"]
  node_creation_api["Creation management<br/>[route.js]"]
  node_creation_item["Single creation<br/>[route.js]"]
  node_database[("PostgreSQL data<br/>[schema.prisma]")]
end

subgraph group_payments["Credit billing"]
  node_pricing["Credit packs<br/>[page.js]"]
  node_checkout["Checkout endpoint<br/>[route.js]"]
  node_billing["Billing service<br/>[billing.js]"]
  node_stripe_webhook["Stripe webhook<br/>[route.js]"]
end

node_user(("User"))
node_muapi{{"MuAPI AI"}}
node_stripe{{"Stripe"}}

node_user -->|"chooses styles"| node_studio
node_studio -.->|"uploads portrait"| node_image_upload
node_image_upload -->|"uploads image"| node_muapi
node_studio -.->|"submits parameters"| node_generation
node_generation -->|"deducts credits"| node_user_service
node_generation -->|"requests image"| node_muapi
node_generation -->|"saves creation"| node_database
node_muapi -->|"sends result"| node_muapi_callback
node_muapi_callback -.->|"updates result"| node_database
node_user -->|"signs in"| node_login
node_login -.->|"starts authentication"| node_auth_route
node_auth_route -->|"uses options"| node_auth
node_auth -->|"persists accounts"| node_database
node_navbar -->|"saves key"| node_apikey
node_apikey -->|"stores key"| node_database
node_gallery -.->|"browses designs"| node_creation_api
node_creation_api -->|"lists and deletes"| node_database
node_creation_item -->|"reads and deletes"| node_database
node_creation_api -->|"checks status"| node_muapi
node_user -->|"chooses pack"| node_pricing
node_pricing -.->|"starts purchase"| node_checkout
node_checkout -->|"creates session"| node_billing
node_billing -->|"creates checkout"| node_stripe
node_stripe -->|"reports completion"| node_stripe_webhook
node_stripe_webhook -->|"handles event"| node_billing
node_billing -->|"adds credits"| node_user_service
node_user_service -->|"updates balances"| node_database

click node_studio "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/page.js"
click node_image_upload "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/upload/route.js"
click node_generation "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/generation/route.js"
click node_muapi_callback "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/webhook/muapi/route.js"
click node_login "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/login/page.js"
click node_auth "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/lib/auth.js"
click node_auth_route "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/auth/%5B...nextauth%5D/route.js"
click node_navbar "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/components/Navbar.js"
click node_apikey "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/user/apikey/route.js"
click node_user_service "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/lib/services/user.js"
click node_gallery "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/gallery/page.js"
click node_creation_api "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/creations/route.js"
click node_creation_item "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/creations/%5Bid%5D/route.js"
click node_database "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/prisma/schema.prisma"
click node_pricing "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/pricing/page.js"
click node_checkout "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/checkout/route.js"
click node_billing "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/lib/services/billing.js"
click node_stripe_webhook "https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/stripe/webhook/route.js"

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_studio,node_image_upload,node_generation,node_muapi_callback,node_user toneBlue
class node_login,node_auth,node_auth_route,node_navbar,node_apikey,node_user_service toneAmber
class node_gallery,node_creation_api,node_creation_item,node_database,node_muapi toneMint
class node_pricing,node_checkout,node_billing,node_stripe_webhook toneRose
class node_stripe toneIndigo
```

## Components

### Makeover workflow

- [Makeover studio](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/page.js): `src/app/page.js`
- [Portrait upload](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/upload/route.js): `src/app/api/upload/route.js`
- [Hairstyle generation](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/generation/route.js): `src/app/api/generation/route.js`
- [Generation callback](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/webhook/muapi/route.js): `src/app/api/webhook/muapi/route.js`

### Accounts and credits

- [Login page](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/login/page.js): `src/app/login/page.js`
- [Authentication](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/lib/auth.js): `src/lib/auth.js`
- [Auth endpoint](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/auth/%5B...nextauth%5D/route.js): `src/app/api/auth/[...nextauth]/route.js`
- [Account navigation](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/components/Navbar.js): `src/components/Navbar.js`
- [API key management](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/user/apikey/route.js): `src/app/api/user/apikey/route.js`
- [Credit operations](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/lib/services/user.js): `src/lib/services/user.js`

### Showroom and storage

- [Personal gallery](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/gallery/page.js): `src/app/gallery/page.js`
- [Creation management](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/creations/route.js): `src/app/api/creations/route.js`
- [Single creation](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/creations/%5Bid%5D/route.js): `src/app/api/creations/[id]/route.js`
- [PostgreSQL data](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/prisma/schema.prisma): `prisma/schema.prisma`

### Credit billing

- [Credit packs](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/pricing/page.js): `src/app/pricing/page.js`
- [Checkout endpoint](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/checkout/route.js): `src/app/api/checkout/route.js`
- [Billing service](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/lib/services/billing.js): `src/lib/services/billing.js`
- [Stripe webhook](https://github.com/samuraigpt/ai-hair-style-simulator/blob/main/src/app/api/stripe/webhook/route.js): `src/app/api/stripe/webhook/route.js`

### Other components

- **User**
- **MuAPI AI**
- **Stripe**

## Connections

- User → Makeover studio: chooses styles (evidence: `README.md`)
- Makeover studio → Portrait upload: uploads portrait
- Portrait upload → MuAPI AI: uploads image (evidence: `src/app/api/upload/route.js`)
- Makeover studio → Hairstyle generation: submits parameters
- Hairstyle generation → Credit operations: deducts credits (evidence: `src/app/api/generation/route.js`)
- Hairstyle generation → MuAPI AI: requests image (evidence: `src/app/api/generation/route.js`)
- Hairstyle generation → PostgreSQL data: saves creation (evidence: `src/app/api/generation/route.js`)
- MuAPI AI → Generation callback: sends result (evidence: `README.md`)
- Generation callback → PostgreSQL data: updates result
- User → Login page: signs in (evidence: `README.md`)
- Login page → Auth endpoint: starts authentication
- Auth endpoint → Authentication: uses options (evidence: `src/app/api/auth/[...nextauth]/route.js`)
- Authentication → PostgreSQL data: persists accounts (evidence: `src/lib/auth.js`)
- Account navigation → API key management: saves key (evidence: `src/components/Navbar.js`)
- API key management → PostgreSQL data: stores key (evidence: `src/app/api/user/apikey/route.js`)
- Personal gallery → Creation management: browses designs
- Creation management → PostgreSQL data: lists and deletes (evidence: `src/app/api/creations/route.js`)
- Single creation → PostgreSQL data: reads and deletes (evidence: `src/app/api/creations/[id]/route.js`)
- Creation management → MuAPI AI: checks status (evidence: `src/app/api/creations/route.js`)
- User → Credit packs: chooses pack (evidence: `README.md`)
- Credit packs → Checkout endpoint: starts purchase
- Checkout endpoint → Billing service: creates session (evidence: `src/app/api/checkout/route.js`)
- Billing service → Stripe: creates checkout (evidence: `src/lib/services/billing.js`)
- Stripe → Stripe webhook: reports completion (evidence: `README.md`)
- Stripe webhook → Billing service: handles event (evidence: `src/app/api/stripe/webhook/route.js`)
- Billing service → Credit operations: adds credits (evidence: `src/lib/services/billing.js`)
- Credit operations → PostgreSQL data: updates balances (evidence: `src/lib/services/user.js`)
