services:
  db:
    image: pgvector/pgvector:pg18
    shm_size: 1g
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: sangita_grantha
    ports:
      - "5432:5432"
    volumes:
      # postgres:18+ layout: single mount at /var/lib/postgresql (data lives in
      # a versioned subdirectory, enabling in-place pg_upgrade). Mounting the
      # old .../data path makes fresh volumes fail to initialize.
      # Migrating from the pre-18 `pgdata` volume: see
      # application_documentation/00-onboarding/getting-started.md (pg_dump →
      # new volume → pg_restore); the old volume is left intact for rollback.
      - pgdata18:/var/lib/postgresql
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 5s
      timeout: 5s
      retries: 5

  # Run migrations before any app service starts (Flyway Community, ADR-013 / TRACK-110).
  # Applies V__ versioned schema + R__ repeatable reference data from one location.
  migrate:
    profiles: [ "dev", "prod" ]
    image: flyway/flyway:13.7.0-alpine
    command: migrate
    environment:
      FLYWAY_URL: jdbc:postgresql://db:5432/sangita_grantha
      FLYWAY_USER: postgres
      FLYWAY_PASSWORD: postgres
      FLYWAY_LOCATIONS: filesystem:/flyway/sql
      FLYWAY_BASELINE_ON_MIGRATE: "false"
      FLYWAY_VALIDATE_MIGRATION_NAMING: "true"
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./database/migrations:/flyway/sql:ro

  # One-shot admin provisioning (argon2id). Not in the default dev stack: requires ADMIN_PASSWORD.
  # Run with BOTH profiles (depends_on migrate, which lives in dev/prod):
  #   ADMIN_PASSWORD=... docker compose --profile dev --profile bootstrap run --rm bootstrap-admin
  bootstrap-admin:
    profiles: [ "bootstrap" ]
    image: eclipse-temurin:25-jdk
    working_dir: /app
    command: ./gradlew :modules:backend:api:bootstrapAdmin --no-daemon
    environment:
      DB_HOST: db
      DB_PORT: 5432
      DB_NAME: sangita_grantha
      DB_USER: postgres
      DB_PASSWORD: postgres
      GRADLE_USER_HOME: /gradle-cache
      ADMIN_EMAIL: ${ADMIN_EMAIL:-admin@sangitagrantha.org}
      # Left empty by default so the whole compose file still parses for unrelated commands;
      # BootstrapAdmin fails fast if ADMIN_PASSWORD is blank.
      ADMIN_PASSWORD: ${ADMIN_PASSWORD:-}
    depends_on:
      migrate:
        condition: service_completed_successfully
    volumes:
      - .:/app
      - gradle-cache:/gradle-cache

  # Dev backend: mounts source + Gradle cache, runs via gradlew (fast incremental builds)
  backend:
    profiles: [ "dev" ]
    image: eclipse-temurin:25-jdk
    working_dir: /app
    command: ./gradlew :modules:backend:api:run --no-daemon
    ports:
      - "8080:8080"
    environment:
      DB_HOST: db
      DB_PORT: 5432
      DB_NAME: sangita_grantha
      DB_USER: postgres
      DB_PASSWORD: postgres
      GRADLE_USER_HOME: /gradle-cache
    depends_on:
      db:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    volumes:
      - .:/app
      - gradle-cache:/gradle-cache

  # Prod backend: builds fat JAR via Dockerfile
  backend-prod:
    profiles: [ "prod" ]
    build:
      context: .
      dockerfile: modules/backend/api/Dockerfile
    ports:
      - "8080:8080"
    environment:
      DB_HOST: db
      DB_PORT: 5432
      DB_NAME: sangita_grantha
      DB_USER: postgres
      DB_PASSWORD: postgres
    depends_on:
      db:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully

  extraction:
    profiles: [ "dev", "prod" ]
    build:
      context: ./tools/krithi-extract-enrich-worker
    environment:
      DATABASE_URL: postgresql://postgres:postgres@db:5432/sangita_grantha
    depends_on:
      db:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    volumes:
      - ./tools/krithi-extract-enrich-worker/src:/app/src:ro
      - extraction-cache:/app/cache
      - ./data/pdfs:/app/pdfs:ro

  frontend:
    profiles: [ "dev" ]
    build:
      context: ./modules/frontend/sangita-admin-web
      dockerfile: Dockerfile.dev
    ports:
      - "5001:5001"
    volumes:
      - ./modules/frontend/sangita-admin-web/src:/app/src:ro
    environment:
      VITE_API_URL: http://localhost:8080
      API_PROXY_TARGET: http://backend:8080

volumes:
  # pg18+ single-mount layout; the retired pre-18 `pgdata` volume is not
  # referenced anymore — remove it manually once the migration is confirmed:
  #   docker volume rm sangeetha-grantha_pgdata
  pgdata18:
  gradle-cache:
  extraction-cache:
