services:
  app:
    profiles: [prod]
    build:
      context: .
      dockerfile: Dockerfile
    image: tranquil-pds
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgres://postgres:postgres@db:5432/pds
    volumes:
      - ./config.toml:/etc/tranquil-pds/config.toml:ro
      - blob_data:/var/lib/tranquil-pds/blobs
      - store_data:/var/lib/tranquil-pds/store
    depends_on:
      db:
        condition: service_healthy

  app-dev:
    profiles: [dev]
    build:
      context: .
      dockerfile: Dockerfile.dev
    command: >
      sh -c '
        cp /certs/pds.test.pem /usr/local/share/ca-certificates/pds.test.crt
        update-ca-certificates
        cargo watch -x "run -p tranquil-server --features native-tls-roots -- --config /app/config.dev.toml"
      '
    environment:
      SERVER_HOST: "[::]"
      SQLX_OFFLINE: "true"
      PLC_DIRECTORY_URL: "http://plc:2582"
    volumes:
      - ./:/app
      - cargo_target:/app/target
      - cargo_registry:/usr/local/cargo/registry
      - blob_data:/var/lib/tranquil-pds/blobs
      - certs:/certs:ro
    depends_on:
      traefik:
        condition: service_started
      db:
        condition: service_healthy
      plc:
        condition: service_started

  frontend-dev:
    profiles: [dev]
    image: node:24-alpine
    working_dir: /app
    command: sh -c "corepack enable && corepack prepare pnpm@latest --activate && pnpm install --ignore-scripts && pnpm dev --host 0.0.0.0"
    environment:
      CI: "true"
      npm_config_store_dir: "/pnpm-store"
      VITE_API_URL: "http://app-dev:3000"
      VITE_HMR_PROTOCOL: "wss"
      VITE_HMR_HOST: "pds.test"
      VITE_HMR_PORT: "443"
    volumes:
      - ./frontend:/app
      - node_modules:/app/node_modules
      - pnpm_store:/pnpm-store
    depends_on:
      - app-dev

  db:
    image: postgres:18-alpine
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: pds
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql
      - ./scripts/init-dev-dbs.sql:/docker-entrypoint-initdb.d/init-dev-dbs.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d pds"]
      interval: 5s
      timeout: 5s
      retries: 10
      start_period: 5s

  plc:
    profiles: [dev]
    build:
      context: https://github.com/did-method-plc/did-method-plc.git
      dockerfile: packages/server/Dockerfile
    environment:
      DB_CREDS_JSON: '{"username":"postgres","password":"postgres","host":"db","port":"5432","database":"plc"}'
      ENABLE_MIGRATIONS: "true"
      DB_MIGRATE_CREDS_JSON: '{"username":"postgres","password":"postgres","host":"db","port":"5432","database":"plc"}'
      PORT: "2582"
    depends_on:
      db:
        condition: service_healthy

  mailpit:
    profiles: [dev]
    image: axllent/mailpit:latest
    ports:
      - "8025:8025"
    environment:
      MP_SMTP_AUTH_ACCEPT_ANY: "true"
      MP_SMTP_AUTH_ALLOW_INSECURE: "true"

  traefik:
    profiles: [dev]
    image: traefik:v3.4
    entrypoint: ["/bin/sh", "-c"]
    command:
      - |
        if [ ! -f /certs/pds.test.pem ]; then
          apk add --no-cache openssl
          openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \
            -keyout /certs/pds.test-key.pem \
            -out /certs/pds.test.pem \
            -days 3650 -nodes \
            -subj "/CN=pds.test" \
            -addext "subjectAltName=DNS:pds.test,DNS:*.pds.test"
          echo "Certs generated"
        fi
        exec traefik \
          --providers.file.filename=/etc/traefik/dynamic.yaml \
          --entrypoints.websecure.address=:443
    ports:
      - "443:443"
    volumes:
      - ./traefik.dev.yaml:/etc/traefik/dynamic.yaml:ro
      - certs:/certs
    networks:
      default:
        aliases:
          - pds.test

  prometheus:
    profiles: [prod]
    image: prom/prometheus:v3.8.0
    ports:
      - "9090:9090"
    volumes:
      - ./observability/prometheus.yaml:/etc/prometheus/prometheus.yaml:ro
      - prometheus_data:/prometheus
    command:
      - "--config.file=/etc/prometheus/prometheus.yaml"
      - "--storage.tsdb.path=/prometheus"
    depends_on:
      - app

volumes:
  postgres_data:
  blob_data:
  store_data:
  prometheus_data:
  cargo_target:
  cargo_registry:
  node_modules:
  pnpm_store:
  certs:
