services:
  redis:
    image: redis:7-alpine
    container_name: timestamps_redis
    ports:
      - "6379:6379"
    command: redis-server --save "" --appendonly no --maxmemory 256mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  web:
    build: .
    container_name: timestamps_web
    command: >
      sh -c "(python manage.py migrate --database=default --noinput || true) &&
             (python manage.py collectstatic --noinput || true) &&
             gunicorn worktimeapp.wsgi:application --bind 0.0.0.0:8484 --workers 3 --timeout 120"
    volumes:
      - .:/app  # Mounts entire project (db.sqlite3 kept for migration/backup)
      - static_volume:/app/staticfiles
    ports:
      - "8484:8484"
    env_file:
      - .env
    environment:
      - SECRET_KEY=${SECRET_KEY:-django-insecure-change-this-in-production}
      - DEBUG=${DEBUG:-False}
      - ALLOWED_HOSTS=${ALLOWED_HOSTS:-localhost,127.0.0.1,0.0.0.0}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_HOST=${MYSQL_HOST}
      - MYSQL_PORT=${MYSQL_PORT:-3306}
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    depends_on:
      redis:
        condition: service_healthy
    restart: unless-stopped

  celery:
    build: .
    container_name: timestamps_celery
    command: celery -A worktimeapp worker --loglevel=info --concurrency=2
    volumes:
      - .:/app  # Mounts entire project
    env_file:
      - .env
    environment:
      - SECRET_KEY=${SECRET_KEY:-django-insecure-change-this-in-production}
      - DEBUG=${DEBUG:-False}
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    depends_on:
      - redis
      - web
    restart: unless-stopped

  celery-beat:
    build: .
    container_name: timestamps_celery_beat
    command: celery -A worktimeapp beat --loglevel=info
    volumes:
      - .:/app  # Mounts entire project
    env_file:
      - .env
    environment:
      - SECRET_KEY=${SECRET_KEY:-django-insecure-change-this-in-production}
      - DEBUG=${DEBUG:-False}
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    depends_on:
      - redis
      - web
    restart: unless-stopped

volumes:
  static_volume:

