# ACI Platform

AI-powered plant grafting scenario modelling for optimizing crop performance across different soil environments.

## Structure

```
aci-foodtech/
├── frontend/   # Next.js 14 (TypeScript, App Router, Tailwind CSS)
└── backend/    # Python FastAPI
```

## Getting Started

### Frontend

```bash
cd frontend
npm install
npm run dev
```

Runs on http://localhost:3000

### Backend

```bash
cd backend
python3 -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env       # fill in your values
uvicorn main:app --reload
```

Runs on http://localhost:8000  
Health check: GET /api/health

### Database

```bash
docker compose up -d db
```

Runs PostgreSQL on port 5433. To apply migrations:

```bash
cd backend
alembic upgrade head
```

To seed plant data:

```bash
python -m app.seeds.enrich --reset           # use cached data
python -m app.seeds.enrich --reset --refresh-cache  # re-fetch from API
```

---

## Deploying to AWS EC2

### 1. Launch an EC2 instance

- **AMI:** Ubuntu 24.04 LTS
- **Instance type:** t3.small minimum, t3.medium recommended
- **Security Group — open ports:** 22 (SSH), 80 (HTTP), 443 (HTTPS)

### 2. Install dependencies on the server

```bash
sudo apt update && sudo apt upgrade -y

# Docker
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker ubuntu
newgrp docker
sudo apt install docker-compose-plugin -y

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

# Python
sudo apt install -y python3 python3-pip python3-venv
```

### 3. Clone and configure

```bash
git clone https://github.com/YOUR_USERNAME/YOUR_REPO.git
cd aci-foodtech
```

Create `backend/.env`:

```
DATABASE_URL=postgresql://aci:aci_secret@localhost:5433/aci_db
ANTHROPIC_API_KEY=sk-...
PERENUAL_API_KEY=...
```

### 4. Start the database

```bash
docker compose up -d db
```

### 5. Install PM2

PM2 manages both processes, auto-restarts on crash, and survives reboots.

```bash
sudo npm install -g pm2
```

### 6. Run the backend with PM2

```bash
cd backend
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
alembic upgrade head

pm2 start "venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000" --name backend
```

### 7. Build and run the frontend with PM2

```bash
cd ../frontend
npm install
npm run build

pm2 start "npm start -- -p 3000" --name frontend
```

### 8. Save PM2 process list and enable startup on reboot

```bash
pm2 save
pm2 startup   # follow the command it prints to enable autostart
```

Useful PM2 commands:

```bash
pm2 list              # see all running processes
pm2 logs backend      # tail backend logs
pm2 logs frontend     # tail frontend logs
pm2 restart backend   # restart after a code change
pm2 stop all          # stop everything
```

### 7. Set up Nginx as a reverse proxy

```bash
sudo apt install nginx -y
```

`/etc/nginx/sites-available/aci`:

```nginx
server {
    listen 80;
    server_name YOUR_DOMAIN_OR_IP;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
    }

    location /api {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
    }
}
```

```bash
sudo ln -s /etc/nginx/sites-available/aci /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl restart nginx
```

### 8. Add HTTPS (optional)

```bash
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com
```

### Pre-launch checklist

- [ ] Set a strong `POSTGRES_PASSWORD` in docker-compose.yml
- [ ] Set `ANTHROPIC_API_KEY` and `PERENUAL_API_KEY` in backend/.env
- [ ] Replace any hardcoded `localhost:8000` in the frontend with your EC2 IP or domain
- [ ] Run `alembic upgrade head` to apply DB migrations
- [ ] Seed the database if needed
