# Development overrides - automatically loaded by docker compose
# Note: Admin services (traefik) have no auth in local dev - auth added via prod overlay
#
# IMPORTANT: For ForwardAuth to work with admin services (n8n, metabase, etc.),
# access the app at http://app.localhost instead of http://localhost:5173.
# This ensures auth cookies are shared across all *.localhost subdomains.
#
# Secrets (DB passwords, JWTs) are read from env vars at runtime — no dev-specific
# SQL files needed for roles or service init. The super admin is created by the
# app at startup from ADMIN_EMAIL / ADMIN_PASSWORD (src/server/auth/bootstrap-admin.ts).

services:
  # Dev stacks cap restarts and don't come back on their own after a reboot:
  # a project folder deleted or moved while its stack existed otherwise
  # crash-loops forever. Run `vibecarbon up` after a reboot. Prod keeps
  # unless-stopped (this file is never deployed).
  db:
    restart: on-failure:5
    ports:
      - "${DEV_DB_PORT:-5432}:5432"
    # WAL-G is off in development. The base file falls back to the app's S3
    # vars, and in dev those name the local S3 on localhost:8333, which inside
    # this container is the container itself: every WAL segment would wait
    # out wal-archive.sh's attempts and then be dropped with a
    # WAL_ARCHIVE_FAILED line. Empty values here win over the base file's
    # interpolation, and an empty WALG_S3_PREFIX_BASE (walg-env.sh then
    # exports an empty WALG_S3_PREFIX) makes wal-archive.sh skip the segment
    # at once with one "no backup target" line.
    environment:
      WALG_S3_PREFIX_BASE: ""
      AWS_ENDPOINT: ""
      AWS_ACCESS_KEY_ID: ""
      AWS_SECRET_ACCESS_KEY: ""

  # Traefik dev config: add extra_hosts so it can reach the Vite dev server on the host
  # Also mount dev middlewares that use host.docker.internal for ForwardAuth
  traefik:
    restart: on-failure:5
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./volumes/traefik:/etc/traefik/dynamic:ro
      # Override middlewares to use host.docker.internal for ForwardAuth in dev
      - ./volumes/traefik/middlewares.dev.yml:/etc/traefik/dynamic/middlewares.yml:ro

  app:
    restart: on-failure:5

  # Local S3 for development uploads (Garage, single node). Dev-only: this file
  # is never deployed; production uses the cloud provider object storage. The
  # browser PUTs to presigned URLs on this port, so it is published to the host.
  #
  # Garage sets itself up on every start, so there is no init container:
  # --single-node assigns the one-node layout when none is stored, and
  # --default-bucket imports the fixed dev key below and creates the bucket
  # with read/write/owner for it, skipping whatever already exists. The key
  # and bucket must match the S3_* values dev-init.js and `vibecarbon create`
  # write (tests/unit/template/dev-s3-credentials-agree.test.ts). The bucket
  # gets a global alias this way, which Garage needs to apply the bucket's
  # CORS to browser preflights; scripts/ensure-dev-bucket.js (run from
  # docker-up) puts the same CORS rule deploy sets on real buckets.
  s3:
    image: dxflrs/garage:v2.5.0@sha256:677e5b758e4425a325b811c425c026bf35a4bf42f7ddbaa773de3f4fd779a5a5
    container_name: ${PROJECT_NAME}-s3
    restart: on-failure:5
    command: ["/garage", "server", "--single-node", "--default-bucket"]
    environment:
      GARAGE_DEFAULT_ACCESS_KEY: GK6c6f63616c2d6465762d7333
      GARAGE_DEFAULT_SECRET_KEY: 6c6f63616c2d6465762d73332d7365637265742d6e6f742d666f722d70726f64
      GARAGE_DEFAULT_BUCKET: ${PROJECT_NAME}-local
    # docker-up.js sets DEV_S3_PORT (8333 + DEV_PORT_OFFSET unless set).
    ports:
      - "127.0.0.1:${DEV_S3_PORT:-8333}:3900"
    volumes:
      - ./volumes/s3/garage.toml:/etc/garage.toml:ro
      - s3_data:/var/lib/garage
    networks:
      - vibecarbon-network

volumes:
  s3_data:
