# ─────────────────────────────────────────────────────────────────────────────
# Xpeditis — Full Dev Stack (infrastructure + app + logging)
#
# Usage:
#   docker-compose -f docker-compose.full.yml up -d
#
# Exposed ports:
#   - Frontend:     http://localhost:3000
#   - Backend:      http://localhost:4000  (Swagger: /api/docs)
#   - Grafana:      http://localhost:3030  (admin / xpeditis_grafana)
#   - Loki:         http://localhost:3100  (internal)
#   - Promtail:     http://localhost:9080  (internal)
#   - log-exporter: http://localhost:3200
#   - MinIO:        http://localhost:9001  (console)
# ─────────────────────────────────────────────────────────────────────────────

services:
  # ─── Infrastructure ────────────────────────────────────────────────────────
  postgres:
    image: postgres:15-alpine
    container_name: xpeditis-postgres-dev
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: xpeditis_dev
      POSTGRES_USER: xpeditis
      POSTGRES_PASSWORD: xpeditis_dev_password
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U xpeditis"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - xpeditis-network

  redis:
    image: redis:7-alpine
    container_name: xpeditis-redis-dev
    ports:
      - "6379:6379"
    command: redis-server --requirepass xpeditis_redis_password
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - xpeditis-network

  minio:
    image: minio/minio:latest
    container_name: xpeditis-minio-dev
    ports:
      - "9000:9000"
      - "9001:9001"
    command: server /data --console-address ":9001"
    volumes:
      - minio_data:/data
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    networks:
      - xpeditis-network

  # ─── Application ───────────────────────────────────────────────────────────
  backend:
    build:
      context: ../apps/backend
      dockerfile: Dockerfile
    container_name: xpeditis-backend-dev
    ports:
      - "4000:4000"
    labels:
      logging: promtail
      logging.service: backend
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    env_file:
      - ../apps/backend/.env
    environment:
      NODE_ENV: development
      LOG_FORMAT: json
      PORT: 4000
      API_PREFIX: api/v1

      # Database
      DATABASE_HOST: postgres
      DATABASE_PORT: 5432
      DATABASE_USER: xpeditis
      DATABASE_PASSWORD: xpeditis_dev_password
      DATABASE_NAME: xpeditis_dev
      DATABASE_SYNC: false
      DATABASE_LOGGING: true

      # Redis
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: xpeditis_redis_password
      REDIS_DB: 0

      # JWT (must be at least 32 characters — enforced at startup)
      JWT_SECRET: dev-secret-jwt-key-for-docker-local-only-0123456789
      JWT_ACCESS_EXPIRATION: 15m
      JWT_REFRESH_EXPIRATION: 7d

      # S3/MinIO
      AWS_S3_ENDPOINT: http://minio:9000
      AWS_REGION: us-east-1
      AWS_ACCESS_KEY_ID: minioadmin
      AWS_SECRET_ACCESS_KEY: minioadmin
      AWS_S3_BUCKET: xpeditis-csv-rates

      # CORS
      CORS_ORIGIN: "http://localhost:3000,http://localhost:4000"

      # Application URL
      APP_URL: http://localhost:3000

      # Security
      BCRYPT_ROUNDS: 10
      SESSION_TIMEOUT_MS: 7200000

      # Rate Limiting
      RATE_LIMIT_TTL: 60
      RATE_LIMIT_MAX: 100

      # SMTP (Brevo)
      SMTP_HOST: smtp-relay.brevo.com
      SMTP_PORT: 587
      SMTP_USER: 9637ef001@smtp-brevo.com
      SMTP_PASS: xsmtpsib-8d965bda028cd63bed868a119f9e0330485204bf9f4e1f92a3a11c8e61000722-xUYUSrGGxhMqlUcu
      SMTP_SECURE: "false"
      SMTP_FROM: noreply@xpeditis.com
    networks:
      - xpeditis-network

  frontend:
    build:
      context: ../apps/frontend
      dockerfile: Dockerfile
      args:
        NEXT_PUBLIC_API_URL: http://localhost:4000
    container_name: xpeditis-frontend-dev
    ports:
      - "3000:3000"
    labels:
      logging: promtail
      logging.service: frontend
    depends_on:
      - backend
    environment:
      NEXT_PUBLIC_API_URL: http://localhost:4000
    networks:
      - xpeditis-network

  # ─── Logging Stack ─────────────────────────────────────────────────────────
  loki:
    image: grafana/loki:3.0.0
    container_name: xpeditis-loki
    restart: unless-stopped
    ports:
      - '3100:3100'
    volumes:
      - ../infra/logging/loki/loki-config.yml:/etc/loki/local-config.yaml:ro
      - loki_data:/loki
    command: -config.file=/etc/loki/local-config.yaml
    healthcheck:
      test: ['CMD-SHELL', 'wget --quiet --tries=1 --spider http://localhost:3100/ready || exit 1']
      interval: 15s
      timeout: 5s
      retries: 5
    networks:
      - xpeditis-network

  promtail:
    image: grafana/promtail:3.0.0
    container_name: xpeditis-promtail
    restart: unless-stopped
    ports:
      - '9080:9080'
    volumes:
      - ../infra/logging/promtail/promtail-config.yml:/etc/promtail/config.yml:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
    command: -config.file=/etc/promtail/config.yml
    depends_on:
      loki:
        condition: service_healthy
    networks:
      - xpeditis-network

  grafana:
    image: grafana/grafana:11.0.0
    container_name: xpeditis-grafana
    restart: unless-stopped
    ports:
      - '3030:3000'
    environment:
      GF_SECURITY_ADMIN_USER: admin
      GF_SECURITY_ADMIN_PASSWORD: xpeditis_grafana
      GF_USERS_ALLOW_SIGN_UP: 'false'
      GF_AUTH_ANONYMOUS_ENABLED: 'false'
      GF_SERVER_ROOT_URL: http://localhost:3030
      GF_ANALYTICS_REPORTING_ENABLED: 'false'
      GF_ANALYTICS_CHECK_FOR_UPDATES: 'false'
    volumes:
      - ../infra/logging/grafana/provisioning:/etc/grafana/provisioning:ro
      - grafana_data:/var/lib/grafana
    depends_on:
      loki:
        condition: service_healthy
    healthcheck:
      test: ['CMD-SHELL', 'wget --quiet --tries=1 --spider http://localhost:3000/api/health || exit 1']
      interval: 15s
      timeout: 5s
      retries: 5
    networks:
      - xpeditis-network

  log-exporter:
    build:
      context: ../apps/log-exporter
      dockerfile: Dockerfile
    container_name: xpeditis-log-exporter
    restart: unless-stopped
    ports:
      - '3200:3200'
    environment:
      PORT: 3200
      LOKI_URL: http://loki:3100
      # Optional: set LOG_EXPORTER_API_KEY to require x-api-key header
      # LOG_EXPORTER_API_KEY: your-secret-key-here
    depends_on:
      loki:
        condition: service_healthy
    networks:
      - xpeditis-network

volumes:
  postgres_data:
  redis_data:
  minio_data:
  loki_data:
    driver: local
  grafana_data:
    driver: local

networks:
  xpeditis-network:
    name: xpeditis-network
