name: calendars

services:
  # Shared PostgreSQL for all services (calendar, caldav server, keycloak)
  postgresql:
    image: postgres:15
    ports:
      - "8912:5432"
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB" ]
      interval: 1s
      timeout: 2s
      retries: 300
    env_file:
      - env.d/development/postgresql.defaults
      - env.d/development/postgresql.local

  redis:
    image: redis:5
    ports:
      - "6379:6379"

  mailcatcher:
    image: sj26/mailcatcher:latest
    ports:
      - "1081:1080"

  backend-dev:
    build:
      context: src/backend
      target: backend-development
      args:
        DOCKER_USER: ${DOCKER_USER:-1000}
    user: ${DOCKER_USER:-1000}
    image: calendars:backend-development
    environment:
      - PYLINTHOME=/app/.pylint.d
      - DJANGO_CONFIGURATION=Development
    env_file:
      - env.d/development/backend.defaults
      - env.d/development/backend.local
    ports:
      - "8921:8000"
    volumes:
      - ./src/backend:/app
      - ./data/static:/data/static
      - /app/.venv
    networks:
      - lasuite
      - default
    depends_on:
      postgresql:
        condition: service_healthy
        restart: true
      mailcatcher:
        condition: service_started
      redis:
        condition: service_started
      celery-dev:
        condition: service_started
      caldav:
        condition: service_started

  celery-dev:
    user: ${DOCKER_USER:-1000}
    image: calendars:backend-development
    networks:
      - default
      - lasuite
    command: [ "celery", "-A", "calendars.celery_app", "worker", "-l", "DEBUG" ]
    environment:
      - DJANGO_CONFIGURATION=Development
    env_file:
      - env.d/development/backend.defaults
      - env.d/development/backend.local
    volumes:
      - ./src/backend:/app
      - ./data/static:/data/static
      - /app/.venv

  nginx:
    image: nginx:1.25
    ports:
      - "8923:8083"
    networks:
      default: {}
      lasuite:
        aliases:
          - nginx
    volumes:
      - ./docker/files/development/etc/nginx/conf.d:/etc/nginx/conf.d:ro
    depends_on:
      - keycloak
      - backend-dev

  frontend-dev:
    user: "${DOCKER_USER:-1000}"
    build:
      context: src/frontend
      target: calendars-dev
      args:
        API_ORIGIN: "http://localhost:8921"
    image: calendars:frontend-development
    env_file:
      - env.d/development/frontend.defaults
      - env.d/development/frontend.local
    volumes:
      - ./src/frontend/:/home/frontend/
      - /home/frontend/node_modules
      - /home/frontend/apps/calendars/node_modules
    ports:
      - "8920:3000"

  crowdin:
    image: crowdin/cli:3.16.0
    volumes:
      - ".:/app"
    env_file:
      - env.d/development/crowdin.defaults
      - env.d/development/crowdin.local
    user: "${DOCKER_USER:-1000}"
    working_dir: /app

  node:
    image: node:22
    user: "${DOCKER_USER:-1000}"
    environment:
      HOME: /tmp
    volumes:
      - ".:/app"

  # CalDAV Server
  caldav:
    build:
      context: docker/sabredav
    ports:
      - "8922:80"
    env_file:
      - env.d/development/caldav.defaults
      - env.d/development/caldav.local
    networks:
      - default
      - lasuite
    depends_on:
      postgresql:
        condition: service_healthy
    command: >
      sh -c "
        /usr/local/bin/init-database.sh || true &&
        apache2-foreground
      "

  # Keycloak - now using shared PostgreSQL
  keycloak:
    image: quay.io/keycloak/keycloak:26.3.2
    volumes:
      - ./docker/auth/realm.json:/opt/keycloak/data/import/realm.json
    command:
      - start-dev
      - --features=preview
      - --import-realm
      - --hostname=http://localhost:8925
      - --hostname-strict=false
    env_file:
      - env.d/development/keycloak.defaults
      - env.d/development/keycloak.local
    ports:
      - "8925:8080"
    depends_on:
      postgresql:
        condition: service_healthy
    networks:
      - lasuite
      - default

networks:
  default: {}
  lasuite:
    name: lasuite-network
    driver: bridge
    external: true
