# DMS Container v0_7_0
# version: "3.8"
x-restart: &restart_policy
  restart: unless-stopped

x-logging: &default_logging
  logging:
    driver: "json-file"
    options:
      max-size: "10m"
      max-file: "5"

volumes:
  backend_node_modules:
  frontend_node_modules:
  frontend_next:
  frontend_next_cache:
services:
  mariadb:
    <<: [*restart_policy, *default_logging]
    image: mariadb:10.11
    container_name: dms_mariadb
    stdin_open: true
    tty: true
    deploy:
      resources:
        limits:
          cpus: "2.0"
          memory: 4G
        reservations:
          cpus: "0.5"
          memory: 1G
    environment:
      MYSQL_ROOT_PASSWORD: "Center#2025"
      MYSQL_DATABASE: "dms"
      MYSQL_USER: "center"
      MYSQL_PASSWORD: "Center#2025"
      TZ: "Asia/Bangkok"
    expose:
      - "80"
    networks: [dmsnet]
    volumes:
      - "/share/Container/dms/mariadb/data:/var/lib/mysql"
      - "/share/Container/dms/mariadb/my.cnf:/etc/mysql/conf.d/my.cnf:ro"
      - "/share/Container/dms/mariadb/init:/docker-entrypoint-initdb.d:ro"
      - "/share/dms-data/mariadb/backup:/backup"
    healthcheck:
      test:
        ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -pCenter#2025 || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 15
  backend:
    <<: [*restart_policy, *default_logging]
    image: dms-backend:dev
    # pull_policy: never  # <-- FINAL FIX ADDED HERE
    container_name: dms_backend
    stdin_open: true
    tty: true
    #user: "node"
    user: "1000:1000"
    working_dir: /app
    deploy:
      resources:
        limits:
          cpus: "2.0"
          memory: 1G
        reservations:
          cpus: "0.25"
          memory: 256M
    environment:
      TZ: "Asia/Bangkok"
      CHOKIDAR_USEPOLLING: "1"
      CHOKIDAR_INTERVAL: "300"
      WATCHPACK_POLLING: "true"
      # NODE_ENV: "production"
      NODE_ENV: "development"
      PORT: "3001"
      DB_HOST: "mariadb"
      DB_PORT: "3306"
      DB_USER: "center"
      DB_PASSWORD: "Center#2025"
      DB_NAME: "dms"
      JWT_SECRET: "9a6d8705a6695ab9bae4ca1cd46c72a6379aa72404b96e2c5b59af881bb55c639dd583afdce5a885c68e188da55ce6dbc1fb4aa9cd4055ceb51507e56204e4ca"
      JWT_ACCESS_SECRET: "9a6d8705a6695ab9bae4ca1cd46c72a6379aa72404b96e2c5b59af881bb55c639dd583afdce5a885c68e188da55ce6dbc1fb4aa9cd4055ceb51507e56204e4ca"
      JWT_REFRESH_SECRET: "743e798bb10d6aba168bf68fc3cf8eff103c18bd34f1957a3906dc87987c0df139ab72498f2fe20d6c4c580f044ccba7d7bfa4393ee6035b73ba038f28d7480c"
      ACCESS_TTL_MS: "900000"
      REFRESH_TTL_MS: "604800000"
      JWT_EXPIRES_IN: "12h"
      PASSWORD_SALT_ROUNDS: "10"
      FRONTEND_ORIGIN: "https://lcbp3.np-dms.work"
      CORS_ORIGINS: "https://lcbp3.np-dms.work,http://localhost:3000,http://127.0.0.1:3000"
      COOKIE_DOMAIN: ".np-dms.work"
      RATE_LIMIT_WINDOW_MS: "900000"
      RATE_LIMIT_MAX: "200"
      BACKEND_LOG_DIR: "/app/logs"
    expose:
      - "3001"
    networks: [dmsnet]
    volumes:
      - "/share/Container/dms/backend/src:/app/src:rw"
      # - "/share/Container/dms/backend/package.json:/app/package.json"
      # - "/share/Container/dms/backend/package-lock.json:/app/package-lock.json"
      - "/share/dms-data:/share/dms-data:rw"
      - "/share/Container/dms/logs/backend:/app/logs:rw"
      # - "/share/Container/dms/backend/node_modules:/app/node_modules"
      - "backend_node_modules:/app/node_modules"
    depends_on:
      mariadb:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://127.0.0.1:3001/health"]
      interval: 10s
      timeout: 5s
      retries: 30
  frontend:
    <<: [*restart_policy, *default_logging]
    image: dms-frontend:dev
    # pull_policy: never  # <-- FINAL FIX ADDED HERE
    container_name: dms_frontend
    stdin_open: true
    tty: true
    # user: "node"
    user: "1000:1000"
    working_dir: /app
    deploy:
      resources:
        limits:
          cpus: "2.0"
          memory: 2G
    environment:
      TZ: "Asia/Bangkok"
      NODE_ENV: "development"
      # NEXT_PUBLIC_API_BASE: "/api"
      CHOKIDAR_USEPOLLING: "1"
      WATCHPACK_POLLING: "true"
      NEXT_PUBLIC_API_BASE: "https://lcbp3.np-dms.work"
      NEXT_PUBLIC_AUTH_MODE: "cookie"
      NEXT_PUBLIC_DEBUG_AUTH: "1"
      NEXT_TELEMETRY_DISABLED: "1"
      INTERNAL_API_BASE: "http://backend:3001"
      JWT_ACCESS_SECRET: "9a6d8705a6695ab9bae4ca1cd46c72a6379aa72404b96e2c5b59af881bb55c639dd583afdce5a885c68e188da55ce6dbc1fb4aa9cd4055ceb51507e56204e4ca"
      JWT_REFRESH_SECRET: "743e798bb10d6aba168bf68fc3cf8eff103c18bd34f1957a3906dc87987c0df139ab72498f2fe20d6c4c580f044ccba7d7bfa4393ee6035b73ba038f28d7480c"
    expose:
      - "3000"
    networks: [dmsnet]
    volumes:
      - "/share/Container/dms/frontend:/app:rw"
      - "frontend_node_modules:/app/node_modules"
      - "frontend_next_cache:/app/.next/cache"
      #- "/share/Container/dms/frontend/node_modules:/app/node_modules"
      - "frontend_next:/app/.next"
      - "/share/Container/dms/logs/frontend:/app/.logs"
    depends_on:
      backend:
        condition: service_healthy
    healthcheck:
      test:
        [
          "CMD-SHELL",
          'wget -qO- http://127.0.0.1:3000/health | grep -q ''"ok":true''',
        ]
      interval: 15s
      timeout: 5s
      retries: 30
  phpmyadmin:
    <<: [*restart_policy, *default_logging]
    image: phpmyadmin:5-apache
    container_name: dms_phpmyadmin
    stdin_open: true
    tty: true
    deploy:
      resources:
        limits:
          cpus: "0.25"
          memory: 256M
    environment:
      TZ: "Asia/Bangkok"
      PMA_HOST: "mariadb"
      PMA_PORT: "3306"
      PMA_ABSOLUTE_URI: "https://pma.np-dms.work/"
      UPLOAD_LIMIT: "256M"
      MEMORY_LIMIT: "512M"
    expose:
      - "80"
    networks: [dmsnet]
    volumes:
      - "/share/Container/dms/phpmyadmin/config.user.inc.php:/etc/phpmyadmin/config.user.inc.php:ro"
      - "/share/Container/dms/phpmyadmin/zzz-custom.ini:/usr/local/etc/php/conf.d/zzz-custom.ini:ro"
      - "/share/Container/dms/phpmyadmin/sessions:/sessions:rw"
      - "/share/Container/dms/phpmyadmin/tmp:/var/lib/phpmyadmin/tmp:rw"
      - "/share/Container/dms/logs/phpmyadmin:/var/log/apache2"
    depends_on:
      mariadb:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://127.0.0.1/"]
      interval: 15s
      timeout: 5s
      retries: 20
  postgres_n8n:
    <<: [*restart_policy, *default_logging]
    image: postgres:16-alpine
    container_name: dms_postgres_n8n
    stdin_open: true
    tty: true
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: 2G
        reservations:
          cpus: "0.5"
          memory: 1G
    environment:
      POSTGRES_DB: "n8n"
      POSTGRES_USER: "n8n"
      POSTGRES_PASSWORD: "Center#2025"
      TZ: "Asia/Bangkok"
    networks: [dmsnet]
    volumes:
      - "/share/Container/dms/n8n-postgres:/var/lib/postgresql/data"
      - "/share/Container/dms/logs/postgres_n8n:/var/log/postgresql"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U n8n -d n8n"]
      interval: 10s
      timeout: 5s
      retries: 10
  pgadmin:
    <<: [*restart_policy, *default_logging]
    image: dpage/pgadmin4:latest
    container_name: dms_pgadmin
    deploy:
      resources:
        limits:
          cpus: "0.25"
          memory: 256M
    environment:
      PGADMIN_DEFAULT_EMAIL: "center.pslcp3@gmail.com"
      PGADMIN_DEFAULT_PASSWORD: "Center#2025"
      PGADMIN_CONFIG_PROXY_X_FOR_COUNT: "1"
      PGADMIN_CONFIG_PROXY_X_PROTO_COUNT: "1"
      PGADMIN_CONFIG_PROXY_X_HOST_COUNT: "1"
      PGADMIN_CONFIG_PROXY_X_PORT_COUNT: "1"
      PGADMIN_CONFIG_PROXY_X_PREFIX_COUNT: "1"
    expose:
      - "80"
    networks: [dmsnet]
    volumes:
      - "/share/Container/dms/pgadmin:/var/lib/pgadmin"
      - "/share/Container/dms/logs/pgadmin:/var/log/pgadmin"
    depends_on:
      postgres_n8n:
        condition: service_healthy
  n8n:
    <<: [*restart_policy, *default_logging]
    image: n8nio/n8n:latest
    container_name: dms_n8n
    stdin_open: true
    tty: true
    deploy:
      resources:
        limits:
          cpus: "1.5"
          memory: 2G
        reservations:
          cpus: "0.25"
          memory: 512M
    environment:
      TZ: "Asia/Bangkok"
      NODE_ENV: "production"
      N8N_PATH: "/n8n/"
      N8N_PUBLIC_URL: "https://n8n.np-dms.work/"
      WEBHOOK_URL: "https://n8n.np-dms.work/"
      N8N_EDITOR_BASE_URL: "https://n8n.np-dms.work/"
      N8N_PROTOCOL: "https"
      N8N_HOST: "n8n.np-dms.work"
      N8N_PORT: "5678"
      N8N_PROXY_HOPS: "1"
      N8N_DIAGNOSTICS_ENABLED: "false"
      N8N_SECURE_COOKIE: "true"
      N8N_ENCRYPTION_KEY: "9AAIB7Da9DW1qAhJE5/Bz4SnbQjeAngI"
      N8N_BASIC_AUTH_ACTIVE: "true"
      N8N_BASIC_AUTH_USER: "n8n"
      N8N_BASIC_AUTH_PASSWORD: "Center#2025"
      GENERIC_TIMEZONE: "Asia/Bangkok"
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres_n8n
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_DATABASE: n8n
      DB_POSTGRESDB_USER: n8n
      DB_POSTGRESDB_PASSWORD: Center#2025
    expose:
      - "5678"
    networks: [dmsnet]
    volumes:
      - "/share/Container/dms/n8n:/home/node/.n8n"
      - "/share/Container/dms/n8n-cache:/home/node/.cache"
      - "/share/Container/dms/scripts:/scripts"
      - "/share/Container/dms/n8n-cache:/data"
      - "/var/run/docker.sock:/var/run/docker.sock"
    depends_on:
      postgres_n8n:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://127.0.0.1:5678/"]
      interval: 15s
      timeout: 5s
      retries: 30
  npm:
    <<: [*restart_policy, *default_logging]
    image: jc21/nginx-proxy-manager:latest
    container_name: dms_npm
    stdin_open: true
    tty: true
    deploy:
      resources:
        limits:
          cpus: "1.0" # 50% CPU
          memory: 512M
    ports:
      - "80:80" # HTTP
      - "443:443" # HTTPS
      - "81:81" # NPM Admin UI
    environment:
      TZ: "Asia/Bangkok"
    networks:
      - dmsnet
      - giteanet
    volumes:
      - "/share/Container/dms/npm/data:/data"
      - "/share/Container/dms/npm/letsencrypt:/etc/letsencrypt"
      - "/share/Container/dms/npm/custom:/data/nginx/custom" # <-- สำคัญสำหรับ http_top.conf
      - "/share/Container/dms/landing:/data/landing:ro"
    depends_on:
      backend:
        condition: service_healthy
      # frontend:
      #  condition: service_healthy
      phpmyadmin:
        condition: service_started
      n8n:
        condition: service_started
      pgadmin:
        condition: service_started
  nginx:
    <<: [*restart_policy, *default_logging]
    image: nginx:1.27-alpine
    container_name: dms_landing
    stdin_open: true
    tty: true
    deploy:
      resources:
        limits:
          cpus: "0.50" # 50% CPU
          memory: 128M
    networks: [dmsnet]
    volumes:
      - "/share/Container/dms/landing:/usr/share/nginx/html:ro"
networks:
  dmsnet: {}
  giteanet:
    external: true
    name: gitnet
# docker network connect git_gitea_net dms_npm
