version: '3.8'

services:
  postgres:
    image: postgres:16-alpine
    container_name: loan_postgres
    environment:
      POSTGRES_DB: loan
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: secret
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - loan_network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    container_name: loan_redis
    networks:
      - loan_network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
    restart: unless-stopped

  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: loan_pgadmin
    ports:
      - "5050:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: admin
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    networks:
      - loan_network
    depends_on:
      - postgres
    restart: unless-stopped

  websocket:
    build:
      context: ./websocket-server
      dockerfile: Dockerfile
    container_name: loan_websocket
    environment:
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_USER=postgres
      - DB_PASSWORD=secret
      - DB_NAME=loan
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - WS_PORT=8080
    networks:
      - loan_network
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/health"]
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 20s

  loan-generator:
    build:
      context: ./websocket-server
      dockerfile: Dockerfile.loan-generator
    container_name: loan_generator
    environment:
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_USER=postgres
      - DB_PASSWORD=secret
      - DB_NAME=loan
      - WS_URL=http://websocket:8080/broadcast
    networks:
      - loan_network
    depends_on:
      postgres:
        condition: service_healthy
      websocket:
        condition: service_healthy
    restart: unless-stopped

  matching-engine:
    build:
      context: ./websocket-server
      dockerfile: Dockerfile.matching-engine
    container_name: loan_matching_engine
    environment:
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_USER=postgres
      - DB_PASSWORD=secret
      - DB_NAME=loan
      - ENGINE_PORT=8083
    networks:
      - loan_network
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "--spider", "-q", "http://localhost:8083/health"]
      interval: 15s
      timeout: 10s
      retries: 5
      start_period: 30s

  loan-client:
    build:
      context: ./loan_client
      dockerfile: Dockerfile.prod
      shm_size: '2gb'
    container_name: loan_client
    volumes:
      - loan_client_storage:/var/www/html/storage
    networks:
      - loan_network
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "--spider", "-q", "http://localhost/"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 60s

  loan-panel:
    build:
      context: ./loan_panel
      dockerfile: Dockerfile.prod
      shm_size: '2gb'
    container_name: loan_panel
    volumes:
      - loan_panel_storage:/var/www/html/storage
    networks:
      - loan_network
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "--spider", "-q", "http://localhost/"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 60s

  nginx-proxy:
    image: nginx:alpine
    container_name: loan_nginx_proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx-proxy/conf.d:/etc/nginx/conf.d:ro
      - certbot_www:/var/www/certbot:ro
      - certbot_certs:/etc/letsencrypt:ro
    networks:
      - loan_network
    depends_on:
      - loan-client
      - loan-panel
      - websocket
    restart: unless-stopped

#  certbot:
#    image: certbot/certbot
#    container_name: loan_certbot
#    volumes:
#      - certbot_www:/var/www/certbot
#      - certbot_certs:/etc/letsencrypt
#    networks:
#      - loan_network

volumes:
  postgres_data:
  pgadmin_data:
  loan_client_storage:
  loan_panel_storage:
  certbot_www:
  certbot_certs:

networks:
  loan_network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.25.0.0/16