services:
  
  # Production service running the built static client and server
  pok:
    profiles: [prod]
    container_name: pok
    hostname: pok-server # If you change this from 'pok-server', update client/src/setupProxy.js and client/vite.config.js
    build:
      context: .
      dockerfile: Dockerfile
      target: pok-server
      args:
        - HOST_UID=${HOST_UID:-1000}
        - HOST_GID=${HOST_GID:-1000}
    restart: always
    env_file:
      - .env
    ports:
      - 8930:8930
    volumes:
      - ./db:/db:rw
      - ./server/routes:/server/routes:ro
      - ./client/build:/server/public/ketr.ketran:ro
    working_dir: /server
    environment:
      - VITE_BASEPATH=/ketr.ketran

  pok-server:
    profiles: [dev]
    container_name: pok.server
    hostname: pok-server # If you change this from 'pok-server', update client/src/setupProxy.js and client/vite.config.js
    build:
      context: .
      dockerfile: Dockerfile
      target: pok-server
      args:
        - HOST_UID=${HOST_UID:-1000}
        - HOST_GID=${HOST_GID:-1000}
    volumes:
      - ./db:/db:rw
      - ./server:/server:rw
    command: ["sh", "-c", "cd /server && npm install --no-audit --no-fund --silent && npm rebuild sqlite3 && npm run start:dev"]
    ports:
      - 8930:8930
    environment:
      - NODE_ENV=development
      - VITE_BASEPATH=/ketr.ketran
    networks:
      - pok-network

  pok-client:
    profiles: [dev]
    container_name: pok.client
    hostname: pok-client
    build:
      context: .
      dockerfile: Dockerfile
      target: pok-client
      args:
        - HOST_UID=${HOST_UID:-1000}
        - HOST_GID=${HOST_GID:-1000}
        - VITE_BASEPATH=${VITE_BASEPATH}
    working_dir: /client
    volumes:
      - ./client:/client:rw
      - ./certs:/certs:ro
    ports:
      - 3001:3001
      - 3003:3003
    environment:
      - BROWSER=none
      - HTTPS=true
      - HOST=0.0.0.0
      # Provide paths to TLS key/cert inside the container so Vite can present
      # a certificate for the external hostname (e.g. battle-linux.ketrenos.com).
      - VITE_HTTPS_KEY=/certs/battle.key
      - VITE_HTTPS_CERT=/certs/battle.crt
    env_file:
      - .env
    # Install deps then run vite directly to avoid using the npm wrapper which
    # was receiving SIGTERM in the container and making the service appear to
    # restart repeatedly. For debugging we avoid --silent so errors are visible.
    command: ["bash", "-c", "cd /client && npm install --legacy-peer-deps --no-fund && ./node_modules/.bin/vite --host"]
    networks:
      - pok-network

  pok-test:
    profiles: [dev,test]
    container_name: pok.test
    hostname: pok-test
    build:
      context: .
      dockerfile: Dockerfile
      target: pok-test
      args:
        - HOST_UID=${HOST_UID:-1000}
        - HOST_GID=${HOST_GID:-1000}
    working_dir: /opt/puppeteer-test
    # Mount the workspace so test artifacts (screenshots) are written back to host
    volumes:
      - ./:/workspace:rw
      - ./tools/puppeteer-test:/opt/puppeteer-test:rw
      - /dev/shm:/dev/shm
    environment:
      # Default to the client service hostname on the compose network so tests
      # can reach the dev client without using host networking.
      - TEST_URL=https://pok-client:3001/ketr.ketran/
      - CHROME_PATH=/usr/bin/chromium
    entrypoint: ["/bin/sh", "-c", "echo Waiting for $${TEST_URL:-https://localhost:3001/ketr.ketran/} to be reachable && \
      for i in $(seq 1 10); do if curl -k -sSf $${TEST_URL:-https://localhost:3001/ketr.ketran/} >/dev/null 2>&1; then echo url up; break; else echo waiting...; sleep 0.5; fi; done; \
      node test.js $${TEST_URL:-https://localhost:3001/ketr.ketran/}"]
    networks:
      - pok-network

networks:
  pok-network:
    driver: bridge
