services:
  postgres:
    image: postgres:18-alpine
    container_name: edubox-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: edubox
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: edubox
    volumes:
      - pg_data:/var/lib/postgresql
    networks:
      - edubox
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U edubox -d edubox"]
      interval: 5s
      timeout: 5s
      retries: 5

  server:
    build:
      context: ./server
      dockerfile: Dockerfile
    container_name: edubox-server
    volumes:
      - ./server/public:/app/public:ro
    cap_add:
      - NET_ADMIN
    command: >
      sh -c "ip route add 100.64.0.0/10 via $$(ip route | awk '/default/ {{print $$3}}') || true && exec node_modules/.bin/next start"
    restart: unless-stopped
    environment:
      DATABASE_URL: ${DATABASE_URL}
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET}
      NEXTAUTH_URL: ${NEXTAUTH_URL}
      SUPERADMIN_EMAIL: ${SUPERADMIN_EMAIL}
      SUPERADMIN_PASSWORD: ${SUPERADMIN_PASSWORD}
      HEADSCALE_URL: ${HEADSCALE_URL}
      HEADSCALE_AUTH_KEY: ${HEADSCALE_AUTH_KEY}
      MAIN_DOMAIN: ${MAIN_DOMAIN}
      GITEA_URL: ${GITEA_URL}
      GITEA_TOKEN: ${GITEA_TOKEN}
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - edubox


  caddy:
    image: caddy:2-alpine
    container_name: edubox-caddy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - ./agent:/usr/share/caddy/agent:ro
      - caddy_data:/data
      - caddy_config:/config
    networks:
      - edubox

  headscale:
    image: headscale/headscale:latest
    container_name: edubox-headscale
    restart: unless-stopped
    command: serve
    ports:
      - "8080:8080"
      - "3478:3478/udp"
    volumes:
      - ./headscale:/etc/headscale
    networks:
      - edubox

  gitea:
    image: gitea/gitea:latest
    container_name: edubox-gitea
    restart: unless-stopped
    ports:
      - "3001:3000"
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - GITEA__database__DB_TYPE=sqlite3
      - GITEA__database__PATH=/data/gitea/gitea.db
    volumes:
      - gitea_data:/data
    networks:
      - edubox

volumes:
  pg_data:
  caddy_data:
  caddy_config:
  headscale_data:
  gitea_data:

networks:
  edubox:
    driver: bridge
