services:
  certbot:
    build:
      context: ./certbot
      dockerfile: Dockerfile
    container_name: certbot
    volumes:
      - ./certbot/config:/etc/letsencrypt/config
      - certbot_data:/etc/letsencrypt
      - certbot_www:/var/www/certbot
    env_file:
      - .env
    environment:
      - EMAIL=${EMAIL}
      - DOMAINS=${ALL_DOMAINS}
      - STAGING=0
    restart: unless-stopped

  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    env_file: .env
    container_name: nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - certbot_data:/etc/letsencrypt
      - certbot_www:/var/www/certbot
      - ./stubSite:/usr/share/nginx/stub/html
      - ./yalarba/serv_spa/spa/vue/dist:/usr/share/nginx/yalarba/html
      - ./valitovgaziz/html:/usr/share/nginx/valitovgaziz/html
      - ./BB/bbvue/dist:/usr/share/nginx/begushiybashkir/html
    networks:
      - web-network
      - internal
      - app-network
      - bb-network
    depends_on:
      - certbot
      - api
      - api_bb

  api:
    build:
      context: ./yalarba/api_tp
      dockerfile: Dockerfile
    ports:
      - "8888:8080"
    container_name: api_tp
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      # Database connection settings
      DB_HOST: db
      DB_PORT: 5432
      DB_USER: postgres
      DB_PASSWORD: postgres
      DB_NAME: mydb
      APP_PORT: 8080
    networks:
      - app-network
    healthcheck:
      test:
        [
          "CMD",
          "wget",
          "--no-verbose",
          "--tries=1",
          "--spider",
          "http://localhost:8080/health",
        ]
      interval: 3000s
      timeout: 10s
      retries: 3

  db:
    image: postgres:15-alpine
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: mydb
    container_name: db_tp
    volumes:
      - db_tp_data:/var/lib/postgresql/data
      - ./migrations:/docker-entrypoint-initdb.d
    networks:
      - app-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 10s
      retries: 5

  api_bb:
    build:
      context: ./BB/api_bb
      dockerfile: Dockerfile
    ports:
      - "7777:8080"
    container_name: api_bb
    restart: unless-stopped
    depends_on:
      db_bb:
        condition: service_healthy
    env_file:
      - ./BB/api_bb/.env
    volumes:
      - api_bb_uploads:/app/uploads
    environment:
      # Database connection settings
      DB_HOST: db_bb
      DB_PORT: 5432
      DB_USER: postgres
      DB_PASSWORD: postgres
      DB_NAME: bb_db
      APP_PORT: 8080
    networks:
      - bb-network
    healthcheck:
      test:
        [
          "CMD",
          "wget",
          "--no-verbose",
          "--tries=1",
          "--spider",
          "http://localhost:8080/api/health",
        ]
      interval: 3000s
      timeout: 10s
      retries: 3

  db_bb:
    image: postgres:15-alpine
    ports:
      - "5433:5432"
    container_name: db_bb
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: bb_db
    volumes:
      - db_bb_data:/var/lib/postgresql/data
    networks:
      - bb-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 10s
      retries: 5

  easysite:
    build:
      context: ./yalarba/easySite/easySite
      dockerfile: Dockerfile
    container_name: easysite
    restart: unless-stopped
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: production
      HOST: 0.0.0.0
      PORT: 3000
    networks:
      - web-network
      - app-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/"]
      interval: 30s
      timeout: 10s
      retries: 3

  api_es:
    build:
      context: ./yalarba/api_es
      dockerfile: Dockerfile
    container_name: api_es
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      DB_HOST: db
      DB_PORT: 5432
      DB_USER: postgres
      DB_PASSWORD: postgres
      DB_NAME: mydb
      APP_PORT: 8081
    networks:
      - app-network
      - web-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8081/health"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  certbot_data:
  certbot_www:
  db_tp_data:
  db_bb_data:
  api_bb_uploads:

networks:
  web-network:
    driver: bridge
  internal:
    driver: bridge
  app-network:
    driver: bridge
  bb-network:
    driver: bridge

# Эта опция автоматически удаляет orphans
x-remove-orphans: true
