# 🏗️ System Architecture — GOFLOW Enterprise AI OS Portal

**System Architecture Document**
เวอร์ชัน 1.0 · Confidential – GOFLOW Enterprise · อัปเดตล่าสุด: 2026-08-07

---

## 1. ภาพรวมสถาปัตยกรรม

Portal v1.0 เป็นระบบ **Client-Server** แบ่งเป็น 3 ชั้นหลัก:

```
┌─────────────────────────────────────────────────────────┐
│  CLIENT (Browser / Mobile)                               │
│  Next.js 16 · React 19 · TypeScript · TailwindCSS        │
│  Framer Motion · Heroicons · Chart.js                    │
└──────────────────────┬──────────────────────────────────┘
                       │ HTTPS / REST + WebSocket
┌──────────────────────▼──────────────────────────────────┐
│  API GATEWAY + BFF (Fastify)                             │
│  Auth · Rate Limit · Sanitize · Helmet · CSRF            │
├──────────────────────────────────────────────────────────┤
│  SERVICES                                               │
│  ┌──────────┐ ┌───────────┐ ┌───────────┐ ┌──────────┐  │
│  │ AI Core  │ │ Identity  │ │ Commerce  │ │ Booking  │  │
│  │ (NEXUS)  │ │  Service  │ │  Service  │ │ Service  │  │
│  └────┬─────┘ └─────┬─────┘ └─────┬─────┘ └────┬─────┘  │
│       │             │             │             │        │
├───────┼─────────────┼─────────────┼─────────────┼────────┤
│  DATA LAYER                                            │
│  PostgreSQL (หลัก) · Redis (cache/session)              │
│  MinIO (ไฟล์/RAG) · Qdrant (vector)                    │
└─────────────────────────────────────────────────────────┘
```

---

## 2. Tech Stack (ตาม Developer Execution Kit)

### Frontend

| เทคโนโลยี | เวอร์ชัน | ใช้สำหรับ |
|---|---|---|
| Next.js | 16 | Framework หลัก (App Router, SSR/SSG/ISR) |
| React | 19 | UI Library |
| TypeScript | 5.x | ภาษา (strict mode) |
| TailwindCSS | 4.x | Utility-first styling + Design Tokens |
| Framer Motion | 11+ | Animation ระดับ professional |
| Heroicons | 2.x | ไอคอนชุดหลัก |
| Chart.js | 4.x | กราฟ Dashboard Preview |

### Backend

| เทคโนโลยี | ใช้สำหรับ |
|---|---|
| GOFLOW NEXUS AI | Reasoning Engine หลัก (RAG + Memory Fusion) — มี Production แล้ว |
| Fastify | API Gateway / BFF (เร็ว, plugin-based) |
| PostgreSQL | ฐานข้อมูลหลัก (relational) |
| Redis | Cache, Session, Rate Limit, Queue |
| MinIO | Object storage (เอกสาร, รูป, RAG files) |
| Qdrant | Vector database สำหรับ Semantic Search |
| Ollama | Local LLM (ตัวเลือกสำหรับ Private Deployment) |
| OpenAI | Cloud LLM (ตัวเลือกสำหรับ SaaS) |

---

## 3. โมดูลและความรับผิดชอบ

| โมดูล | ความรับผิดชอบ | เจ้าของทีม |
|---|---|---|
| Web App (Next.js) | หน้าเว็บทั้งหมด, SSR/SEO, Dashboard Preview | Frontend |
| API Gateway (Fastify) | Routing, Auth, Rate Limit, Validation | Backend |
| AI Core Service | เชื่อม NEXUS AI, RAG, Memory | AI |
| Identity Service | ผู้ใช้, องค์กร, บทบาท, JWT | Backend |
| Commerce Service | Marketplace, Order, Payment (Wallet) | Backend + FinTech |
| Booking Service | จอง Demo, ปฏิทิน, การนัดหมาย | Backend |
| Recruitment Service | ตำแหน่งงาน, ใบสมัคร, Resume upload | Backend |
| Storage Service | MinIO + Qdrant สำหรับเอกสารและความรู้ | AI + DevOps |

---

## 4. AI Integration (NEXUS AI)

Portal เชื่อมต่อกับ **GOFLOW NEXUS AI** ผ่าน REST API (รายละเอียดใน `04-api-specification.md`):

| Endpoint | หน้าที่ |
|---|---|
| `POST /api/ai/chat` | สนทนากับ AI (RAG + Memory Fusion) — **ใช้งานจริงใน Production แล้ว** |
| `POST /api/ai/resolve` | แก้ปัญหา/ตัดสินใจอัตโนมัติ (AI Resolve) |
| `GET /api/knowledge/search` | ค้นความรู้ Semantic Search |
| `GET /api/executive/report` | สร้างรายงานผู้บริหาร |
| `GET /api/executive/tasks` | ภารกิจที่ AI Executive ดูแล |

### Data Flow ของ AI Chat

```
User → Next.js → API Gateway → AI Core Service
                                    │
                              ┌─────▼──────┐
                              │ NEXUS AI   │
                              │ v7.3       │
                              │ RAG +      │
                              │ Memory     │
                              └─────┬──────┘
                      ┌──────────────┼──────────────┐
                      ▼              ▼              ▼
                   Qdrant        PostgreSQL      Redis
                 (knowledge)    (memory/org)    (cache)
```

---

## 5. การจัดการสถานะ (State Management)

| ชั้น | กลไก | หมายเหตุ |
|---|---|---|
| Client | React Server Components + TanStack Query | Server state แยกจาก UI state |
| Cache | Redis | Session, Rate Limit counters, hot data |
| ฐานข้อมูล | PostgreSQL | แหล่งข้อมูลจริง (Source of Truth) |
| ไฟล์ | MinIO | เอกสารและ media (URL อ้างอิงใน DB) |

---

## 6. ความปลอดภัย (Security Architecture)

- **HTTPS** — บังคับทุกคำขอ (HSTS)
- **Helmet** — Security headers (CSP, X-Frame-Options, X-Content-Type-Options)
- **CSP** — Content Security Policy เข้มงวด (script-src จำกัด)
- **Rate Limit** — ต่อ IP/ผู้ใช้ ผ่าน Redis (เช่น 100 req/min สำหรับ API, 10 req/min สำหรับ AI chat)
- **Sanitize** — ตรวจสอบและทำความสะอาด input ทุกจุด (XSS prevention)
- **CSRF** — Token สำหรับ state-changing requests
- **Auth** — JWT (access + refresh), RBAC ตาม Permission Model ของ GOFLOW
- **Secrets** — เก็บใน Environment Variables / Secret Manager ห้าม commit ใน Git

---

## 7. การปรับขนาดและความพร้อมใช้งาน

| ด้าน | แนวทาง |
|---|---|
| Scale | Horizontal scaling ผ่าน Container (Docker/K8s), Stateless services |
| Cache | Redis ลดภาระ DB สำหรับ hot paths |
| HA | Multi-instance + Health Check + Load Balancer |
| Backup | PostgreSQL nightly + WAL, MinIO bucket replication (ดู `08-deployment-guide.md`) |
| Monitoring | Logs (centralized), Metrics (Prometheus), Alerts (incident response) |

---

## 8. ข้อจำกัดและข้อสังเกตของ v1.0

- Portal ตัวปัจจุบันที่ deploy อยู่เป็น **Static Site (HTML/CSS/JS)** เพื่อให้เปิดใช้และนำเสนอได้ทันที
- สถาปัตยกรรมในเอกสารนี้คือ **เป้าหมายของเวอร์ชัน Production (Next.js 16 + Fastify)** ตาม Developer Execution Kit
- การย้ายจาก static → Next.js ทำได้แบบค่อยเป็นค่อยไป โดยใช้หน้าเว็บเดิมเป็นเนื้อหาอ้างอิง
- AI Endpoint `/api/ai/chat` พร้อมเชื่อมต่อได้ทันที (NEXUS AI อยู่ใน Production)

---

*เอกสารนี้เป็นส่วนหนึ่งของ GOFLOW Developer Kit — อ่านคู่กับ 04-api-specification.md และ 05-database-schema.md*
