# System Architecture Documentation

DocValidator is designed as a modular, layered Laravel application. Business logic is strictly kept out of HTTP controllers and placed into dedicated, testable domain services.

---

## Architecture Layers

```
┌────────────────────────────────────────────────────────┐
│                   HTTP / Presentation                  │
│   Web Routes ──> Middleware (Role, Active)            │
│   Controllers: Auth, Client, Admin, Public             │
│   Blade Views + Tailwind CSS v4 + Lucide Icons         │
└──────────────────────────┬─────────────────────────────┘
                           │
┌──────────────────────────▼─────────────────────────────┐
│                     Domain Services                    │
│   DocumentUploadService                                │
│   DocumentAuthenticationService                        │
│   PdfProcessingService (FPDI + TCPDF)                  │
│   QrCodeService (SimpleQrCode / Bacon)                 │
│   NotificationService                                  │
│   AuditLogService                                      │
└──────────────────────────┬─────────────────────────────┘
                           │
┌──────────────────────────▼─────────────────────────────┐
│                    Persistence Layer                   │
│   Eloquent Models + Database Transactions              │
│   Private Storage Disk (storage/app/private)           │
│   MySQL Database (document_authentication)             │
└────────────────────────────────────────────────────────┘
```

---

## Document Authentication Engine Workflow

```
Administrator clicks "Approve & Authenticate"
                    │
                    ▼
   DocumentAuthenticationService::authenticate()
                    │
  ┌─────────────────┴──────────────────┐
  │ 1. Validate status & permissions    │
  │ 2. Verify original file SHA-256     │
  │ 3. Generate unique validation_code  │  (DB Transaction Begin)
  │ 4. Generate 64-char hex token       │
  │ 5. QrCodeService: render PNG QR     │
  │ 6. PdfProcessingService:            │
  │    • FPDI imports original pages    │
  │    • Draws authentication footer    │
  │    • Embeds QR image + code         │
  │ 7. Write authenticated PDF to disk  │
  │ 8. Calculate authenticated SHA-256  │
  │ 9. Insert document_certificates     │
  │ 10. Record approval & events        │
  │ 11. Update document status to approved
  │ 12. Create in-app client notification
  └─────────────────┬──────────────────┘
                    │
           Commit Transaction
         (Rolls back & cleans up
           temp files on failure)
```

---

## Security Model

1. **Private Storage**: Original and authenticated PDFs are placed outside the web root (`storage/app/private`). Downloads require controller-level authorization checks.
2. **Cryptographic Tokens**: Verification URLs use a 64-character cryptographically random token (`bin2hex(random_bytes(32))`), eliminating sequential ID prediction.
3. **MIME Magic Byte Validation**: Uploads are validated using both Laravel rules and binary magic byte inspection (`%PDF`, `\xFF\xD8\xFF`, `\x89PNG`).
4. **Idempotent Authentication**: The authentication engine checks for existing certificates before issuance and wraps all actions in a single atomic database transaction.
5. **No Client Charges**: Free by design — zero payment routes or packages.

