# Inventory - Guide de Déploiement & Release

## Architecture

```
inventory.malio-dev.fr/     → Frontend Nuxt (statique)
inventory.malio-dev.fr/api  → Backend Symfony (PHP-FPM)
```

| Composant | Technologie | Emplacement serveur |
|-----------|-------------|---------------------|
| Backend | Symfony 8 + API Platform | `/var/www/Inventory/` |
| Frontend | Nuxt 4 (statique) | `/var/www/Inventory/Inventory_frontend/.output/public/` |
| Base de données | PostgreSQL 16 | `inventory` |

---

## Prérequis serveur

- **OS** : Ubuntu/Debian
- **PHP** : 8.4 avec extensions : pgsql, intl, zip, gd, mbstring, curl
- **Node.js** : 20+
- **Nginx**
- **PostgreSQL** : 16
- **Composer**

Vérifier :
```bash
php -v                    # PHP 8.4+
php -m | grep -E 'pgsql|intl|zip|gd|mbstring'
node -v                   # Node 20+
nginx -v
psql --version
composer --version
```

---

## Déploiement initial

### 1. Cloner le projet

```bash
cd /var/www
sudo git clone --recurse-submodules gitea@gitea.malio.fr:MALIO-DEV/Inventory.git Inventory
sudo chown -R malio:malio Inventory
cd Inventory
git checkout master
git submodule update --init --recursive
```

### 2. Créer la base de données

```bash
sudo -u postgres psql

CREATE DATABASE inventory OWNER ferme_user;
GRANT ALL PRIVILEGES ON DATABASE inventory TO ferme_user;
\q
```

Importer le dump :
```bash
# Copier le dump depuis le PC local
scp backup_v1.0.0_clean.sql malio@192.168.0.159:/tmp/

# Importer
psql -U ferme_user -h 127.0.0.1 -d inventory -f /tmp/backup_v1.0.0_clean.sql
```

### 3. Configurer le backend Symfony

```bash
cd /var/www/Inventory

# Installer les dépendances
composer install --no-dev --optimize-autoloader

# Créer .env.local
cat > .env.local << 'EOF'
APP_ENV=prod
APP_DEBUG=0
APP_SECRET=CHANGE_ME

DATABASE_URL="postgresql://ferme_user:fermerecette@127.0.0.1:5432/inventory?serverVersion=16"

CORS_ALLOW_ORIGIN='^https?://inventory\.malio-dev\.fr$'

JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
JWT_PASSPHRASE=inventoryjwt
EOF

# Générer APP_SECRET
sed -i "s/CHANGE_ME/$(openssl rand -hex 32)/" .env.local

# Générer les clés JWT
mkdir -p config/jwt
openssl genrsa -out config/jwt/private.pem -aes256 4096
# Passphrase : inventoryjwt
openssl rsa -pubout -in config/jwt/private.pem -out config/jwt/public.pem
chmod 600 config/jwt/private.pem

# Permissions
sudo chown -R www-data:www-data var/
sudo chmod -R 775 var/

# Vider le cache
php bin/console cache:clear --env=prod
```

### 4. Configurer le frontend Nuxt

```bash
cd /var/www/Inventory/Inventory_frontend

# Permissions
sudo chown -R malio:malio .

# Installer les dépendances
npm install

# Créer .env
cat > .env << 'EOF'
NUXT_PUBLIC_API_BASE_URL=http://inventory.malio-dev.fr/api
EOF

# Générer le site statique
npx nuxi generate
```

### 5. Configurer Nginx

```bash
sudo nano /etc/nginx/sites-available/inventory
```

Contenu :
```nginx
server {
    listen 80;
    server_name inventory.malio-dev.fr;

    # Gros fichiers (100MB max)
    client_max_body_size 100M;
    client_body_timeout 300s;
    send_timeout 300s;

    access_log /var/log/nginx/inventory-access.log;
    error_log /var/log/nginx/inventory-error.log;

    # Backend Symfony - /api
    location /api {
        root /var/www/Inventory/public;
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /var/www/Inventory/public/index.php;
        fastcgi_param DOCUMENT_ROOT /var/www/Inventory/public;
        fastcgi_read_timeout 300s;
        internal;
    }

    # Frontend statique
    location / {
        root /var/www/Inventory/Inventory_frontend/.output/public;
        index index.html;
        try_files $uri $uri/ /index.html;
    }
}
```

Activer :
```bash
sudo ln -s /etc/nginx/sites-available/inventory /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
```

### 6. Vérifier

```bash
curl http://inventory.malio-dev.fr
curl http://inventory.malio-dev.fr/api
```

---

## Mises à jour

### Mettre à jour l'application

```bash
cd /var/www/Inventory

# Pull les changements
git pull
git submodule update --init --recursive

# Backend
composer install --no-dev --optimize-autoloader
php bin/console cache:clear --env=prod
sudo chown -R www-data:www-data var/

# Frontend
cd Inventory_frontend
npm install
npx nuxi generate
```

---

## Versioning & Releases

### Source de vérité

Le fichier `VERSION` à la racine contient le numéro de version (ex: `1.0.0`).

Cette version est synchronisée avec :
- Le footer de l'application
- `config/packages/api_platform.yaml`

### Créer une release

```bash
# Depuis le PC de dev
./scripts/release.sh patch   # 1.0.0 → 1.0.1
./scripts/release.sh minor   # 1.0.0 → 1.1.0
./scripts/release.sh major   # 1.0.0 → 2.0.0
./scripts/release.sh 2.0.0   # Version exacte
```

Le script :
1. Vérifie/commit le submodule frontend
2. Met à jour `VERSION` et `api_platform.yaml`
3. Commit et tag les deux repos
4. Affiche les commandes pour push

### Pousser la release

```bash
# Frontend (submodule)
cd Inventory_frontend && git push && git push --tags && cd ..

# Backend
git push && git push --tags
```

### Créer la release sur Gitea

1. Aller sur le dépôt Gitea
2. **Releases** > **New Release**
3. Sélectionner le tag `vX.Y.Z`
4. Ajouter les notes de release

---

## Commandes utiles

```bash
# Logs Nginx
tail -f /var/log/nginx/inventory-error.log

# Logs Symfony
tail -f /var/www/Inventory/var/log/prod.log

# Vider le cache Symfony
php /var/www/Inventory/bin/console cache:clear --env=prod

# Rebuild frontend
cd /var/www/Inventory/Inventory_frontend && npx nuxi generate

# Status PHP-FPM
systemctl status php8.4-fpm

# Reload Nginx
sudo systemctl reload nginx
```

---

## Backup base de données

### Export
```bash
pg_dump -U ferme_user -h 127.0.0.1 -d inventory --no-owner --no-acl --inserts --column-inserts --clean --if-exists > backup_inventory_$(date +%Y%m%d).sql
```

### Import
```bash
psql -U ferme_user -h 127.0.0.1 -d inventory -f backup_inventory_YYYYMMDD.sql
```

---

## Troubleshooting

### Erreur 502 Bad Gateway
```bash
# Vérifier PHP-FPM
systemctl status php8.4-fpm
sudo systemctl restart php8.4-fpm
```

### Erreur 403 Forbidden
```bash
# Vérifier les permissions
sudo chown -R www-data:www-data /var/www/Inventory/var/
sudo chmod -R 775 /var/www/Inventory/var/
```

### Erreur API "No route found"
```bash
# Vider le cache
php /var/www/Inventory/bin/console cache:clear --env=prod
```

### Frontend ne se met pas à jour
```bash
# Rebuild
cd /var/www/Inventory/Inventory_frontend
rm -rf .output
npx nuxi generate
```
