# Produktionskomposition för befintlig server (spec §51, steg 1 i skalningsplanen).
# Placeras under /opt/app-platform/. Secrets injiceras via .env-fil som
# ägs av appens dedikerade användare (chmod 600) tills SSM-integration (steg 2).
#
# Krav enligt spec §51: egen Linux-user, egna secrets, egna loggar, egna larm,
# egen domän, egen S3-bucket, egen IAM-roll, CPU/RAM-limits. Se docs/infrastruktur.md.
services:
  app-api:
    build:
      context: ../..
      dockerfile: infrastructure/docker/Dockerfile.api
    image: app-api:latest
    container_name: app-api
    env_file: /opt/app-platform/secrets/api.env
    ports:
      - "127.0.0.1:4100:4000" # bakom befintlig reverse proxy
    restart: unless-stopped
    depends_on:
      app-redis:
        condition: service_healthy
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: 768M
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "5"

  app-worker:
    build:
      context: ../..
      dockerfile: infrastructure/docker/Dockerfile.worker
    image: app-worker:latest
    container_name: app-worker
    env_file: /opt/app-platform/secrets/worker.env
    restart: unless-stopped
    depends_on:
      app-redis:
        condition: service_healthy
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: 768M
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "5"

  app-admin:
    build:
      context: ../..
      dockerfile: infrastructure/docker/Dockerfile.admin
      args:
        VITE_API_BASE_URL: https://api.example.invalid # sätt via brand/.env
    image: app-admin:latest
    container_name: app-admin
    ports:
      - "127.0.0.1:4180:8080"
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: "0.25"
          memory: 128M

  app-redis:
    image: redis:7-alpine
    container_name: app-redis
    command:
      [
        "redis-server",
        "--appendonly",
        "yes",
        "--maxmemory",
        "512mb",
        "--maxmemory-policy",
        "noeviction",
      ]
    volumes:
      - app_redis_data:/data
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
    deploy:
      resources:
        limits:
          cpus: "0.5"
          memory: 640M

# PostgreSQL: befintlig RDS-instans med separat separat databas och
# användaren app-användaren med minsta möjliga privilegier (spec §52).
# Se infrastructure/deployment/create-database.sql.

volumes:
  app_redis_data:
