version: '3.8'

services:
  postgres:
    image: postgres:16-alpine
    container_name: katheryn-db
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${DB_USER:-directus}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: ${DB_DATABASE:-katheryn}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - katheryn-internal
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-directus} -d ${DB_DATABASE:-katheryn}"]
      interval: 10s
      timeout: 5s
      retries: 5

  cache:
    image: redis:7-alpine
    container_name: katheryn-cache
    restart: unless-stopped
    networks:
      - katheryn-internal
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  directus:
    image: directus/directus:11
    container_name: katheryn-cms
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      cache:
        condition: service_healthy
    environment:
      # Database
      DB_CLIENT: pg
      DB_HOST: postgres
      DB_PORT: 5432
      DB_DATABASE: ${DB_DATABASE:-katheryn}
      DB_USER: ${DB_USER:-directus}
      DB_PASSWORD: ${DB_PASSWORD}

      # Cache
      CACHE_ENABLED: "true"
      CACHE_STORE: redis
      REDIS: redis://cache:6379

      # Security
      SECRET: ${DIRECTUS_SECRET}
      ADMIN_EMAIL: ${ADMIN_EMAIL}
      ADMIN_PASSWORD: ${ADMIN_PASSWORD}

      # Public URL (for asset URLs)
      PUBLIC_URL: https://katheryn-cms.jeffemmett.com

      # File Storage
      STORAGE_LOCATIONS: local
      STORAGE_LOCAL_ROOT: /directus/uploads

      # CORS for frontend
      CORS_ENABLED: "true"
      CORS_ORIGIN: "https://katheryn-staging.jeffemmett.com,https://katheryntrenshaw.com,http://localhost:3000"

      # Rate limiting
      RATE_LIMITER_ENABLED: "true"
      RATE_LIMITER_STORE: redis
      RATE_LIMITER_REDIS: redis://cache:6379

    volumes:
      - directus_uploads:/directus/uploads
      - directus_extensions:/directus/extensions
    networks:
      - katheryn-internal
      - traefik-public
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.katheryn-cms.rule=Host(`katheryn-cms.jeffemmett.com`)"
      - "traefik.http.routers.katheryn-cms.entrypoints=web"
      - "traefik.http.services.katheryn-cms.loadbalancer.server.port=8055"

volumes:
  postgres_data:
  directus_uploads:
  directus_extensions:

networks:
  katheryn-internal:
    driver: bridge
  traefik-public:
    external: true
