networks:
  storycove-network:
    driver: bridge

services:
  nginx:
    image: nginx:alpine
    ports:
      - "6925:80"
    volumes:
      - /volume1/docker/storycove/images:/app/images:ro
    configs:
      - source: nginx_config
        target: /etc/nginx/nginx.conf
    depends_on:
      - frontend
      - backend
    networks:
      - storycove-network

  frontend:
    build: ./frontend
    environment:
      - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL:-/api}
    depends_on:
      - backend
    networks:
      - storycove-network

  backend:
    build: ./backend
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/storycove
      - SPRING_DATASOURCE_USERNAME=storycove
      - SPRING_DATASOURCE_PASSWORD=${DB_PASSWORD}
      - JWT_SECRET=${JWT_SECRET}
      - SOLR_HOST=solr
      - SOLR_PORT=8983
      - SOLR_SCHEME=http
      - SEARCH_ENGINE=${SEARCH_ENGINE:-solr}
      - IMAGE_STORAGE_PATH=/app/images
      - APP_PASSWORD=${APP_PASSWORD}
      - STORYCOVE_CORS_ALLOWED_ORIGINS=${STORYCOVE_CORS_ALLOWED_ORIGINS:-http://localhost:3000,http://localhost:6925}
    volumes:
      - /volume1/docker/storycove/images:/app/images
      - /volume1/docker/storycove/config:/app/config
      - /volume1/docker/storycove/backups:/app/automatic-backups
    depends_on:
      postgres:
        condition: service_healthy
      solr:
        condition: service_started
    networks:
      - storycove-network

  postgres:
    image: postgres:15-alpine
    # No port mapping - only accessible within the Docker network
    #ports:
    #  - "5432:5432"
    environment:
      - POSTGRES_DB=storycove
      - POSTGRES_USER=storycove
      - POSTGRES_PASSWORD=${DB_PASSWORD}
    volumes:
      - /volume1/docker/storycove/postgres:/var/lib/postgresql/data
    networks:
      - storycove-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U storycove -d storycove"]
      interval: 5s
      timeout: 5s
      retries: 5


  solr:
    build:
      context: .
      dockerfile: solr.Dockerfile
    ports:
      - "8983:8983"  # Expose Solr Admin UI for development
    environment:
      - SOLR_HEAP=1024m
      - SOLR_JAVA_MEM=-Xms512m -Xmx1024m
    volumes:
      - /volume1/docker/storycove/solr:/var/solr
    deploy:
      resources:
        limits:
          memory: 2G
        reservations:
          memory: 1G
    stop_grace_period: 30s
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:8983/solr/admin/ping || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    networks:
      - storycove-network
    restart: unless-stopped


configs:
  nginx_config:
    content: |
      events {
          worker_connections 1024;
      }
      http {
          upstream frontend {
              server frontend:3000;
          }
          upstream backend {
              server backend:8080;
          }
          server {
              listen 80;
              client_max_body_size 2048M;
              location / {
                  proxy_pass http://frontend;
                  proxy_http_version 1.1;
                  proxy_set_header Upgrade $$http_upgrade;
                  proxy_set_header Connection upgrade;
                  proxy_set_header Host $$host;
                  proxy_set_header X-Real-IP $$remote_addr;
                  proxy_set_header X-Forwarded-For $$proxy_add_x_forwarded_for;
                  proxy_set_header X-Forwarded-Proto $$scheme;
                  proxy_cache_bypass $$http_upgrade;
              }
              location /api/ {
                  proxy_pass http://backend/api/;
                  proxy_set_header Host $$host;
                  proxy_set_header X-Real-IP $$remote_addr;
                  proxy_set_header X-Forwarded-For $$proxy_add_x_forwarded_for;
                  proxy_set_header X-Forwarded-Proto $$scheme;
                  proxy_connect_timeout 900s;
                  proxy_send_timeout 900s;
                  proxy_read_timeout 900s;
                  # Large upload settings (2GB for backups)
                  client_max_body_size 2048M;
                  proxy_request_buffering off;
                  proxy_max_temp_file_size 0;
              }
              location /images/ {
                  alias /app/images/;
                  expires 1y;
                  add_header Cache-Control public;
              }
          }
      }