# =============================================================================
# docker-compose.yml — CRM System v1.0 (local testing with PostgreSQL)
#
# Use this file to run a full Postgres + crm-app stack locally, e.g. for
# smoke-testing the Docker image or running the backend against a real
# PostgreSQL instance before deploying to Coolify.
#
# Production deploys in Coolify use COOLIFY_SETUP.md (single-container app +
# a Coolify-managed Postgres database), NOT this file.
#
# Usage:
#   cp .env.docker.example .env.docker
#   $EDITOR .env.docker          # fill AUTH_SECRET, POSTGRES_PASSWORD, ...
#   docker compose --env-file .env.docker up --build
#   curl http://localhost:8000/health
# =============================================================================

services:
  # -------------------------------------------------------------------------
  # PostgreSQL 16 (Alpine) — local Postgres for development / smoke tests.
  # Coolify will provision its own managed Postgres database in production.
  # -------------------------------------------------------------------------
  postgres:
    image: postgres:16-alpine
    container_name: crm-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-crm_user}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
      POSTGRES_DB: ${POSTGRES_DB:-crm_db}
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"   # local-only convenience; remove for CI / prod-like runs
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-crm_user} -d ${POSTGRES_DB:-crm_db}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    networks:
      - crm-net

  # -------------------------------------------------------------------------
  # CRM App — built from the local Dockerfile.
  # NOTE: no `image:` directive — we build from source on `docker compose up`.
  # -------------------------------------------------------------------------
  crm-app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: crm-app
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      # Use the internal docker-compose DNS name "postgres" (NOT localhost)
      DATABASE_URL: ${DATABASE_URL:?DATABASE_URL is required}
      AUTH_SECRET: ${AUTH_SECRET:?AUTH_SECRET is required (min 32 chars)}
      # Frontend served from same origin in production; allow local dev hosts too
      CORS_ORIGINS: ${CORS_ORIGINS:-http://localhost:8000,http://localhost:5173}
      ENVIRONMENT: ${ENVIRONMENT:-production}
      LOG_LEVEL: ${LOG_LEVEL:-INFO}
      # JWT settings — keep aligned with .env.example
      JWT_ALGORITHM: ${JWT_ALGORITHM:-HS256}
      JWT_EXPIRY_HOURS: ${JWT_EXPIRY_HOURS:-24}
      BCRYPT_ROUNDS: ${BCRYPT_ROUNDS:-12}
    ports:
      - "8000:8000"
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 15s
    networks:
      - crm-net

volumes:
  pgdata:
    name: crm_pgdata

networks:
  crm-net:
    name: crm-net
    driver: bridge
