# Deployment

Monetarie Core is designed for deployment on **Google Cloud Platform (GCP)** in the `southamerica-east1` (Sao Paulo) region, optimized for low-latency PIX processing in Brazil.

## Infrastructure Overview

| Component | GCP Service | Configuration |
|-----------|-------------|---------------|
| API Server | Cloud Run | 2-4 vCPU, 2-4 GiB, auto-scaling 1-100 |
| Frontend Apps | Cloud Run + CDN | Static builds served via nginx |
| Database | Cloud SQL PostgreSQL 16 | HA, 8 vCPU, 32 GB, PITR |
| Cache | Memorystore Redis 7.2 | Standard HA, 2-5 GB |
| Message Queue | GKE + NATS | JetStream enabled, 2-3 replicas |
| Ledger | GKE + TigerBeetle | Single node (expandable to 3-6 replicas) |
| Load Balancer | Global HTTPS L7 | CDN enabled, managed SSL |
| Secrets | Secret Manager | Auto-rotation for credentials |
| Monitoring | Cloud Monitoring + Grafana | Custom dashboards and alerts |

## Prerequisites

- GCP project with billing enabled
- `gcloud` CLI installed and authenticated
- `kubectl` configured for GKE cluster access
- Domain DNS configured (e.g., `*.fluxiq.com.br`)
- Docker and container registry access

## Step 1: Infrastructure Setup

### VPC Network

```bash
# Create VPC
gcloud compute networks create monetarie-vpc \
  --subnet-mode=custom \
  --project=YOUR_PROJECT_ID

# Create subnet
gcloud compute networks subnets create monetarie-subnet \
  --network=monetarie-vpc \
  --region=southamerica-east1 \
  --range=10.0.0.0/20
```

### Cloud SQL (PostgreSQL)

```bash
# Create instance
gcloud sql instances create monetarie-db \
  --database-version=POSTGRES_16 \
  --tier=db-custom-8-32768 \
  --region=southamerica-east1 \
  --availability-type=REGIONAL \
  --storage-type=SSD \
  --storage-size=100GB \
  --backup-start-time=03:00 \
  --enable-point-in-time-recovery \
  --network=monetarie-vpc

# Create database
gcloud sql databases create monetarie --instance=monetarie-db
```

### Redis (Memorystore)

```bash
gcloud redis instances create monetarie-cache \
  --size=2 \
  --region=southamerica-east1 \
  --tier=STANDARD_HA \
  --redis-version=redis_7_2 \
  --network=monetarie-vpc
```

### GKE Cluster

```bash
gcloud container clusters create monetarie-cluster \
  --region=southamerica-east1 \
  --machine-type=e2-standard-4 \
  --num-nodes=2 \
  --enable-ip-alias \
  --network=monetarie-vpc \
  --subnetwork=monetarie-subnet
```

## Step 2: Deploy Infrastructure Services

### NATS with JetStream

```bash
kubectl apply -f infrastructure/k8s/nats/nats-deployment.yaml
kubectl apply -f infrastructure/k8s/nats/nats-streams-midaz.yaml
```

### TigerBeetle

```bash
kubectl apply -f infrastructure/k8s/tigerbeetle/namespace.yaml
kubectl apply -f infrastructure/k8s/tigerbeetle/pvc.yaml
kubectl apply -f infrastructure/k8s/tigerbeetle/deployment.yaml
kubectl apply -f infrastructure/k8s/tigerbeetle/service.yaml
```

## Step 3: Build and Deploy Application

```bash
# Build backend image
gcloud builds submit backend/ \
  --tag southamerica-east1-docker.pkg.dev/PROJECT_ID/fluxiq/api:latest

# Deploy to Cloud Run
gcloud run deploy monetarie-api \
  --image=southamerica-east1-docker.pkg.dev/PROJECT_ID/fluxiq/api:latest \
  --region=southamerica-east1 \
  --cpu=2 --memory=2Gi \
  --min-instances=1 --max-instances=100 \
  --vpc-connector=monetarie-connector
```

## Step 4: Load Balancer and SSL

```bash
# Create managed SSL certificate
gcloud compute ssl-certificates create monetarie-cert \
  --domains="api.fluxiq.com.br,banking.fluxiq.com.br,admin.fluxiq.com.br,merchant.fluxiq.com.br"

# Create URL map and HTTPS proxy
gcloud compute url-maps create monetarie-url-map \
  --default-service=monetarie-api-backend

gcloud compute target-https-proxies create monetarie-https-proxy \
  --url-map=monetarie-url-map \
  --ssl-certificates=monetarie-cert
```

## Rollback Procedure

```bash
# List recent revisions
gcloud run revisions list --service=monetarie-api --region=southamerica-east1 --limit=5

# Route traffic to previous revision
gcloud run services update-traffic monetarie-api \
  --to-revisions=monetarie-api-PREVIOUS_REVISION=100 \
  --region=southamerica-east1
```

## Cost Estimate

| Component | Monthly Cost |
|-----------|-------------|
| Cloud SQL (HA, 8 vCPU) | ~$400 |
| Memorystore Redis (HA, 2GB) | ~$120 |
| GKE (2 nodes, e2-standard-4) | ~$200 |
| Cloud Run (API + 3 UIs) | ~$300 |
| Load Balancer + CDN | ~$50 |
| Cloud NAT | ~$45 |
| Monitoring and Logging | ~$85 |
| **Total** | **~$1,200/month** |
