version: "3.8"

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

  directus:
    image: directus/directus:11
    restart: unless-stopped
    ports:
      - "${DIRECTUS_PORT:-8055}:8055"
    depends_on:
      database:
        condition: service_healthy
    environment:
      SECRET: ${SECRET}
      DB_CLIENT: pg
      DB_HOST: database
      DB_PORT: 5432
      DB_DATABASE: ${DB_DATABASE}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      ADMIN_EMAIL: ${ADMIN_EMAIL}
      ADMIN_PASSWORD: ${ADMIN_PASSWORD}
      PUBLIC_URL: "https://clientes.deusens.com/lloret_backend"
      WEBSOCKETS_ENABLED: "true"
      CORS_ENABLED: ${CORS_ENABLED:-true}
      CORS_ORIGIN: ${CORS_ORIGIN:-true}
    volumes:
      - directus_uploads:/directus/uploads
      - directus_extensions:/directus/extensions
      - ./snapshots:/directus/snapshots
    command: >
      sh -c "node /directus/cli.js bootstrap &&
             node /directus/cli.js schema apply --yes /directus/snapshots/lloret-schema.yaml &&
             node /directus/cli.js start"
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8055/server/health | grep -q 'ok'"]
      interval: 10s
      timeout: 10s
      retries: 15
      start_period: 60s

volumes:
  db_data:
  directus_uploads:
  directus_extensions:
