# System Architecture — Legal Decision Assistant

## Components

```
┌─────────────────────────────────────────────────────┐
│                    Nautilus Server                    │
│                  158.178.131.193                      │
│                                                       │
│  ┌──────────┐  ┌──────────┐  ┌──────────────────┐   │
│  │ Coolify   │  │ Traefik  │  │ ezer-mishpati-web│   │
│  │ (manage)  │  │ (proxy)  │  │ (upload UI)      │   │
│  └──────────┘  └──────────┘  └──────────────────┘   │
│                                                       │
│  ┌──────────────────┐  ┌──────────────────────────┐  │
│  │ PostgreSQL        │  │ Redis                     │  │
│  │ + pgvector        │  │ (task queue)              │  │
│  │ (legal-ai-postgres│  │ (legal-ai-redis)          │  │
│  └──────────────────┘  └──────────────────────────┘  │
│                                                       │
│  ┌──────────┐  ┌──────────┐                          │
│  │ Gitea    │  │ n8n      │                          │
│  │ (code)   │  │ (automate│                          │
│  └──────────┘  └──────────┘                          │
│                                                       │
│  ┌──────────────────────────────────────────────┐    │
│  │ Claude Code (via SSH or API)                  │    │
│  │ — Skills: legal-decision, legal-docx          │    │
│  │ — MCP: postgres, n8n, cloudflare, chrome      │    │
│  └──────────────────────────────────────────────┘    │
└─────────────────────────────────────────────────────┘

External:
  ← Claude API (embeddings, analysis)
  ← Cloudflare DNS (*.nautilus.marcusgroup.org)
  ← User (Putty SSH / Browser)
```

## Data Flow

```
1. Document Upload
   User → ezer-mishpati-web → file storage → n8n trigger
   → classify document → store metadata in PostgreSQL
   → generate embeddings → store in pgvector

2. Decision Writing
   Claude Code → read source materials from DB
   → generate structure DOCX (12 blocks)
   → write each block with appropriate model/parameters
   → validate against block-schema
   → export final DOCX

3. Precedent Search (RAG)
   Query → generate embedding → pgvector similarity search
   → return relevant paragraphs/decisions
   → Claude analyzes relevance → present to user
```

## Database Schema — 4 Layers

### Layer 1: Core
appeals, parties, panels, documents

### Layer 2: Decision
decisions, decision_blocks, decision_paragraphs, claims

### Layer 3: Legal Knowledge
case_law, case_law_citations, statutory_provisions, transition_phrases, lessons_learned

### Layer 4: Semantic Search (RAG)
document_embeddings, paragraph_embeddings, case_law_embeddings
(all using pgvector vector(1536) columns)

## Technology Choices
- **Database**: PostgreSQL 15 + pgvector 0.8.1
- **Embedding model**: TBD (Claude/OpenAI ada-002/local)
- **Automation**: n8n (workflow engine)
- **Code repository**: Gitea (self-hosted)
- **Deployment**: Coolify (Docker management)
- **Proxy**: Traefik v3.6 (auto-SSL)
- **Frontend**: ezer-mishpati-web (static HTML + API)
