services:
  # 1. ADATBÁZIS
  postgres-db:
    image: postgres:15
    container_name: postgres-db
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - ./postgres_data:/var/lib/postgresql/data
      - ./postgres/init-db.sql:/docker-entrypoint-initdb.d/init-db.sql
    ports:
      - "${POSTGRES_PORT}:${POSTGRES_PORT}"
    networks:
      - service_finder_net
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 5

  # 2. BACKEND API
  service_finder_api:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: service_finder_api
    volumes:
      - ./backend:/app
      - ./alembic.ini:/app/alembic.ini
      - ./migrations:/app/migrations
    command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --proxy-headers --forwarded-allow-ips="*"
    ports:
      - "8000:8000"
    environment:
      - PYTHONPATH=/app
      - DATABASE_URL=postgresql+asyncpg://${APP_DB_USER}:${APP_DB_PASSWORD}@postgres-db:5432/${APP_DB_NAME}
      - SECRET_KEY=${SECRET_KEY}
      - ALGORITHM=${ALGORITHM}
      - ALLOWED_ORIGINS=https://dev.profibot.hu,
      - MINIO_ENDPOINT=minio:9000
      - MINIO_ACCESS_KEY=${POSTGRES_USER}
      - MINIO_SECRET_KEY=${POSTGRES_PASSWORD}
      - SENDGRID_API_KEY=${SENDGRID_API_KEY}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    depends_on:
      postgres-db:
        condition: service_healthy
      minio:
        condition: service_started
      redis:
        condition: service_started
    networks:
      - service_finder_net

  # 3. PGADMIN
  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin_ui
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD}
    volumes:
      - ./pgadmin_data:/var/lib/pgadmin
    ports:
      - "5050:80"
    depends_on:
      - postgres-db
    networks:
      - service_finder_net

  # 4. MINIO
  minio:
    image: minio/minio
    container_name: service_finder_minio
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: ${POSTGRES_USER}
      MINIO_ROOT_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - /mnt/nas/app_data/minio_data:/data
    ports:
      - "9000:9000"
      - "9001:9001"
    networks:
      - service_finder_net

  # 5. REDIS
  redis:
    image: redis:alpine
    container_name: service_finder_redis
    volumes:
      - /mnt/nas/app_data/redis_data:/data
    networks:
      - service_finder_net

 # 6. CODE-SERVER
  code-server:
    image: codercom/code-server:latest
    container_name: code-server
    user: "0:0"
    volumes:
      - /opt/service_finder:/opt/service_finder
      - /var/run/docker.sock:/var/run/docker.sock
      - /opt/service_finder/.vscode_config:/root
    environment:
      - PASSWORD=${CODE_SERVER_PASSWORD}
      - TERM=xterm-256color
      - TZ=Europe/Budapest
    working_dir: /opt/service_finder
    ports:
      - "8443:8080"
    restart: unless-stopped
    networks:
      - service_finder_net
    # Ez a rész telepíti az eszközöket minden indításkor:
    command: >
      command: >
      sh -c "apt-get update && 
             apt-get install -y ca-certificates curl gnupg mc python3 python3-pip htop &&
             install -m 0755 -d /etc/apt/keyrings &&
             curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg &&
             chmod a+r /etc/apt/keyrings/docker.gpg &&
             echo \"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(. /etc/os-release && echo \"$VERSION_CODENAME\") stable\" | tee /etc/apt/sources.list.d/docker.list > /dev/null &&
             apt-get update &&
             apt-get install -y docker-ce-cli &&
             dumb-init /usr/bin/code-server --bind-addr 0.0.0.0:8080 /opt/service_finder"

  # 7. NGINX PROXY MANAGER
  nginx-proxy:
    image: 'jc21/nginx-proxy-manager:latest'
    container_name: nginx-proxy-manager
    restart: unless-stopped
    ports:
      - '80:80'
      - '81:81'
      - '443:443'
    volumes:
      - ./proxy-manager/data:/data
      - ./proxy-manager/letsencrypt:/etc/letsencrypt
    networks:
      - service_finder_net

  # 8. DOZZLE
  dozzle:
    image: amir20/dozzle:latest
    container_name: dozzle
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - "8888:8080"
    restart: unless-stopped
    networks:
      - service_finder_net

  # 9. Frontend konténer
  service_finder_frontend:
    build:
      context: ./frontend
    container_name: service_finder_frontend
    ports:
      - "3000:80"
    environment:
      - ALLOWED_ORIGINS=https://app.profibot.hu,
    networks:
      - service_finder_net
    depends_on:
      - service_finder_api

networks:
  service_finder_net:
    driver: bridge
