version: '3.8'

services:
  postgres:
    image: postgres:14-alpine
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: sankofa
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
    command: postgres -c shared_preload_libraries=pg_stat_statements

  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    environment:
      DB_HOST: postgres
      DB_PORT: 5432
      DB_NAME: sankofa
      DB_USER: postgres
      DB_PASSWORD: postgres
      JWT_SECRET: dev-secret-change-in-production
      NODE_ENV: development
      # Sovereign Identity (Keycloak) - NO Azure dependencies
      KEYCLOAK_URL: http://keycloak:8080
      KEYCLOAK_REALM: master
      KEYCLOAK_CLIENT_ID: sankofa-api
      KEYCLOAK_CLIENT_SECRET: dev-client-secret
      KEYCLOAK_MULTI_REALM: "false"
      # Multi-Tenancy
      ENABLE_MULTI_TENANT: "true"
      BLOCKCHAIN_IDENTITY_ENABLED: "false"
      # Billing
      BILLING_GRANULARITY: SECOND
      BLOCKCHAIN_BILLING_ENABLED: "false"
    ports:
      - "4000:4000"
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - ./api:/app
      - /app/node_modules
    command: pnpm dev

  keycloak:
    image: quay.io/keycloak/keycloak:latest
    environment:
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: admin
      KC_DB: postgres
      KC_DB_URL_HOST: postgres
      KC_DB_URL_DATABASE: keycloak
      KC_DB_USERNAME: postgres
      KC_DB_PASSWORD: postgres
      KC_HTTP_ENABLED: "true"
    ports:
      - "8080:8080"
    depends_on:
      postgres:
        condition: service_healthy
    command: start-dev

  frontend:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      NEXT_PUBLIC_GRAPHQL_ENDPOINT: http://localhost:4000/graphql
      NODE_ENV: development
    ports:
      - "3000:3000"
    depends_on:
      - api
    volumes:
      - .:/app
      - /app/node_modules
      - /app/.next
    command: pnpm dev

volumes:
  postgres_data:

