# Installation

This guide covers all installation methods for STA Connector.

## System Requirements

### Minimum Requirements

| Component | Requirement |
|-----------|-------------|
| CPU | 2 cores |
| Memory | 2 GB RAM |
| Storage | 10 GB |
| OS | Linux, macOS, or Windows with WSL2 |

### Recommended for Production

| Component | Requirement |
|-----------|-------------|
| CPU | 4+ cores |
| Memory | 8 GB RAM |
| Storage | 50 GB SSD |
| OS | Linux (Ubuntu 22.04+ or RHEL 8+) |

## Docker Installation (Recommended)

### Prerequisites

- Docker Engine 24.0+
- Docker Compose 2.0+

### Step 1: Get the Code

```bash
git clone https://github.com/MonetarieBR/sta
cd sta
```

### Step 2: Configure Environment

Create a `.env` file with your settings:

```bash
# .env
SECRET_KEY_BASE=$(mix phx.gen.secret)
PHX_HOST=your-domain.com
DATABASE_URL=ecto://postgres:your-password@db/sta_connector
```

### Step 3: Build and Start

```bash
# Build images
docker-compose build

# Start services
docker-compose up -d

# Check status
docker-compose ps

# View logs
docker-compose logs -f backend
```

### Step 4: Initialize Database

```bash
docker-compose exec backend bin/sta_connector eval "StaConnector.Release.migrate()"
```

## Native Installation

### Prerequisites

Install the required dependencies:

#### Ubuntu/Debian

```bash
# Add Erlang Solutions repository
wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb
sudo dpkg -i erlang-solutions_2.0_all.deb
sudo apt-get update

# Install Erlang and Elixir
sudo apt-get install -y esl-erlang elixir

# Install PostgreSQL
sudo apt-get install -y postgresql postgresql-contrib

# Install Node.js
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
```

#### macOS

```bash
# Using Homebrew
brew install elixir postgresql node

# Start PostgreSQL
brew services start postgresql
```

#### RHEL/CentOS

```bash
# Add EPEL repository
sudo dnf install -y epel-release

# Install Erlang from Erlang Solutions
sudo dnf install -y erlang elixir

# Install PostgreSQL
sudo dnf install -y postgresql-server postgresql-contrib
sudo postgresql-setup --initdb
sudo systemctl start postgresql
sudo systemctl enable postgresql

# Install Node.js
sudo dnf module install -y nodejs:20
```

### Step 1: Clone Repository

```bash
git clone https://github.com/MonetarieBR/sta
cd sta
```

### Step 2: Setup Backend

```bash
cd backend

# Install Hex and Rebar
mix local.hex --force
mix local.rebar --force

# Install dependencies
mix deps.get

# Compile
mix compile
```

### Step 3: Configure Database

```bash
# Create PostgreSQL user and database
sudo -u postgres psql << EOF
CREATE USER sta_connector WITH PASSWORD 'your-secure-password';
CREATE DATABASE sta_connector_dev OWNER sta_connector;
CREATE DATABASE sta_connector_test OWNER sta_connector;
GRANT ALL PRIVILEGES ON DATABASE sta_connector_dev TO sta_connector;
GRANT ALL PRIVILEGES ON DATABASE sta_connector_test TO sta_connector;
EOF
```

Update `config/dev.exs` with your database credentials:

```elixir
config :sta_connector, StaConnector.Repo,
  username: "sta_connector",
  password: "your-secure-password",
  hostname: "localhost",
  database: "sta_connector_dev"
```

Run migrations:

```bash
mix ecto.create
mix ecto.migrate
```

### Step 4: Setup Frontend

```bash
cd ../frontend

# Install dependencies
npm install

# Build for production (optional)
npm run build
```

### Step 5: Run Services

#### Development Mode

```bash
# Terminal 1 - Backend
cd backend
mix phx.server

# Terminal 2 - Frontend
cd frontend
npm run dev
```

#### Production Mode

```bash
# Build release
cd backend
MIX_ENV=prod mix release

# Run
_build/prod/rel/sta_connector/bin/sta_connector start
```

## Kubernetes Deployment

For Kubernetes deployments, we provide Helm charts:

```bash
# Add the Helm repository
helm repo add sta-connector https://monetarie.github.io/sta-connector/charts
helm repo update

# Install
helm install sta-connector sta-connector/sta-connector \
  --namespace sta-connector \
  --create-namespace \
  --set postgresql.enabled=true \
  --set ingress.enabled=true \
  --set ingress.host=sta.your-domain.com
```

See the [Helm chart documentation](https://github.com/MonetarieBR/sta/tree/main/charts) for all configuration options.

## Verification

After installation, verify all components are working:

```bash
# Backend health check
curl http://localhost:4000/api/health

# Expected response:
# {"status":"healthy","version":"1.0.0"}

# Frontend (should return HTML)
curl http://localhost:3000

# Database connectivity
cd backend && mix ecto.migrations
```

## Upgrading

### Docker Upgrade

```bash
# Pull latest images
docker-compose pull

# Restart services
docker-compose up -d

# Run migrations
docker-compose exec backend bin/sta_connector eval "StaConnector.Release.migrate()"
```

### Native Upgrade

```bash
# Pull latest code
git pull origin main

# Update backend
cd backend
mix deps.get
mix compile
MIX_ENV=prod mix ecto.migrate

# Update frontend
cd ../frontend
npm install
npm run build
```

## Next Steps

- [Configuration Guide](/guide/configuration) - Configure STA endpoints and credentials
- [Architecture Overview](/architecture/) - Understand the system design
