version: "3.8"

# ZGZ Turismo — Directus 10 Local Stack
# Contratos C1 (Exp. 26/2026) + C2 (Exp. 28/2026) + PR_260047
# DeuSens Hyperxperience S.L.

services:
  # ─────────────────────────────────────────
  # PostgreSQL 15
  # ─────────────────────────────────────────
  postgres:
    image: postgres:15-alpine
    container_name: zgz_postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${DB_DATABASE:-zgz_turismo}
      POSTGRES_USER: ${DB_USER:-directus}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-directus_dev_pass}
    volumes:
      - pgdata:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-directus} -d ${DB_DATABASE:-zgz_turismo}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ─────────────────────────────────────────
  # Redis 7
  # ─────────────────────────────────────────
  redis:
    image: redis:7-alpine
    container_name: zgz_redis
    restart: unless-stopped
    command: redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ─────────────────────────────────────────
  # Directus 10
  # ─────────────────────────────────────────
  directus:
    image: directus/directus:10.10.0
    container_name: zgz_directus
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "8056:8055"
    volumes:
      - ../uploads:/directus/uploads
      - ../extensions:/directus/extensions
      - ../snapshots:/directus/snapshots
    environment:
      # ── Core ──
      KEY: ${DIRECTUS_KEY:-zgz-dev-key-change-in-production}
      SECRET: ${DIRECTUS_SECRET:-zgz-dev-secret-change-in-production}
      PUBLIC_URL: ${PUBLIC_URL:-http://localhost:8055}

      # ── Database ──
      DB_CLIENT: pg
      DB_HOST: postgres
      DB_PORT: 5432
      DB_DATABASE: ${DB_DATABASE:-zgz_turismo}
      DB_USER: ${DB_USER:-directus}
      DB_PASSWORD: ${DB_PASSWORD:-directus_dev_pass}

      # ── Cache (memory для локальной разработки) ──
      CACHE_ENABLED: "true"
      CACHE_STORE: memory
      CACHE_TTL: 5m
      CACHE_AUTO_PURGE: "true"

      # ── File storage ──
      STORAGE_LOCATIONS: local
      STORAGE_LOCAL_DRIVER: local
      STORAGE_LOCAL_ROOT: /directus/uploads
      FILES_MAX_UPLOAD_SIZE: 1gb
      FILES_MAX_UPLOAD_CONCURRENCY: 20
      TUS_ENABLED: "true"
      TUS_CHUNK_SIZE: 20mb
      TUS_UPLOAD_EXPIRATION: 1h

      # ── Admin user (first run only) ──
      ADMIN_EMAIL: ${ADMIN_EMAIL:-admin@deusens.com}
      ADMIN_PASSWORD: ${ADMIN_PASSWORD:-Admin1234!}

      # ── CORS (allow PWA + kiosk dev) ──
      CORS_ENABLED: "true"
      CORS_ORIGIN: "http://localhost:5173,http://localhost:5174,http://localhost:3000,http://localhost:8080,http://127.0.0.1:8080,http://localhost:8081,http://127.0.0.1:8081,*.zaragoza.es"
      CORS_METHODS: GET,POST,PATCH,DELETE
      CORS_ALLOWED_HEADERS: Content-Type,Authorization

      # ── Email (nodemailer via SMTP) ──
      EMAIL_FROM: ${EMAIL_FROM:-noreply@zaragoza.es}
      EMAIL_TRANSPORT: smtp
      EMAIL_SMTP_HOST: ${EMAIL_SMTP_HOST:-smtp.gmail.com}
      EMAIL_SMTP_PORT: ${EMAIL_SMTP_PORT:-587}
      EMAIL_SMTP_USER: ${EMAIL_SMTP_USER:-}
      EMAIL_SMTP_PASSWORD: ${EMAIL_SMTP_PASSWORD:-}
      EMAIL_SMTP_SECURE: "false"

      # ── Rate limiting (memory для локальной разработки) ──
      RATE_LIMITER_ENABLED: "false"
      RATE_LIMITER_STORE: memory
      RATE_LIMITER_POINTS: 1000
      RATE_LIMITER_DURATION: 3600
      RATE_LIMITER_GLOBAL_POINTS: 5000

      # ── Telemetry opt-out ──
      TELEMETRY: "false"

      # ── Migrations & Snapshots ──
      MIGRATIONS_PATH: /directus/migrations
      SCHEMA_SYNC_PATH: /directus/snapshots

      # ── Logging ──
      LOG_LEVEL: ${LOG_LEVEL:-info}
      LOG_STYLE: pretty

      # ── Extensions ──
      EXTENSIONS_PATH: /directus/extensions
      EXTENSIONS_AUTO_RELOAD: "true"

    healthcheck:
      test: wget --no-verbose --tries=1 --spider http://localhost:8055/server/health || exit 1
      interval: 15s
      timeout: 10s
      retries: 10
      start_period: 30s

  # ─────────────────────────────────────────
  # Adminer (lightweight DB GUI for dev)
  # ─────────────────────────────────────────
  adminer:
    image: adminer:latest
    container_name: zgz_adminer
    restart: unless-stopped
    depends_on:
      - postgres
    ports:
      - "8080:8080"
    profiles:
      - tools

volumes:
  pgdata:
networks:
  default:
    name: zgz_network
