services:
  towercontrol:
    build:
      context: .
      dockerfile: Dockerfile
      target: runner
    ports:
      - '${PORT:-3007}:3000'
    environment:
      NODE_ENV: ${NODE_ENV:-production}
      DATABASE_URL: ${DATABASE_URL:-file:/app/data/dev.db}
      BACKUP_DATABASE_PATH: ${BACKUP_DATABASE_PATH:-./data/dev.db}
      BACKUP_STORAGE_PATH: ${BACKUP_STORAGE_PATH:-./data/backups}
      TZ: ${TZ:-Europe/Paris}
      # NextAuth.js
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET:-TbwIWAmQgBcOlg7jRZrhkeEUDTpSr8Cj/Cc7W58fAyw=}
      NEXTAUTH_URL: ${NEXTAUTH_URL:-http://localhost:3006}
      # Jira (optionnel)
      JIRA_BASE_URL: ${JIRA_BASE_URL:-}
      JIRA_EMAIL: ${JIRA_EMAIL:-}
      JIRA_API_TOKEN: ${JIRA_API_TOKEN:-}
      # Debug
      VERBOSE_LOGGING: ${VERBOSE_LOGGING:-false}
    volumes:
      - ./data:/app/data # Dossier local data/ vers /app/data
    restart: unless-stopped
    healthcheck:
      test: ['CMD', 'wget', '-qO-', 'http://localhost:3000/api/health']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  towercontrol-dev:
    build:
      context: .
      dockerfile: Dockerfile
      target: base
    ports:
      - '${PORT_DEV:-3005}:3000'
    environment:
      NODE_ENV: ${NODE_ENV:-development}
      DATABASE_URL: ${DATABASE_URL:-file:/app/data/dev.db}
      BACKUP_DATABASE_PATH: ${BACKUP_DATABASE_PATH:-./data/dev.db}
      BACKUP_STORAGE_PATH: ${BACKUP_STORAGE_PATH:-./data/backups}
      TZ: ${TZ:-Europe/Paris}
      # NextAuth.js
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET:-TbwIWAmQgBcOlg7jRZrhkeEUDTpSr8Cj/Cc7W58fAyw=}
      NEXTAUTH_URL: ${NEXTAUTH_URL:-http://localhost:3005}
      # Jira (optionnel)
      JIRA_BASE_URL: ${JIRA_BASE_URL:-}
      JIRA_EMAIL: ${JIRA_EMAIL:-}
      JIRA_API_TOKEN: ${JIRA_API_TOKEN:-}
      # Debug
      VERBOSE_LOGGING: ${VERBOSE_LOGGING:-false}
    volumes:
      - .:/app # code en live
      - /app/node_modules # vol anonyme pour ne pas écraser ceux du conteneur
      - /app/.next
      - ./data:/app/data # Dossier local data/ vers /app/data
    command: >
      sh -c "pnpm install &&
             pnpm prisma generate &&
             (pnpm prisma migrate deploy || (echo 'Migration failed, using db push for fresh database...' && pnpm prisma db push --accept-data-loss --skip-generate && for migration in prisma/migrations/*/; do if [ -d \"\$migration\" ] && [ -f \"\$migration/migration.sql\" ]; then migration_name=\$(basename \"\$migration\"); pnpm prisma migrate resolve --applied \"\$migration_name\" 2>/dev/null || true; fi; done)) &&
             pnpm run dev"
    profiles:
      - dev
# 📁 Structure des données :
# ./data/          -> /app/data (bind mount)
# ├── prod.db      -> Base de données production
# ├── dev.db       -> Base de données développement
# └── backups/     -> Sauvegardes automatiques
#
# 🔧 Configuration via variables d'environnement (.env ou .env.local)
# Les variables utilisent la syntaxe ${VAR:-default} pour les fallbacks
# 📚 Documentation : ./data/README.md et env.example
