version: '3.8'

services:
  # Main FastAPI application
  {{ project_name }}:
    build:
      context: .
      dockerfile: Dockerfile
      target: development  # Use development stage by default
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/{{ project_name }}
      - REDIS_URL=redis://redis:6379/0
      - ENVIRONMENT=development
    volumes:
      - .:/app
      - /app/__pycache__
      - /app/.pytest_cache
    depends_on:
      - postgres
      - redis
    networks:
      - {{ project_name }}-network
    restart: unless-stopped

  # Production build
  {{ project_name }}-prod:
    build:
      context: .
      dockerfile: Dockerfile
      target: base  # Use production stage
    ports:
      - "8001:8000"
    environment:
      - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/{{ project_name }}
      - REDIS_URL=redis://redis:6379/0
      - ENVIRONMENT=production
    depends_on:
      - postgres
      - redis
    networks:
      - {{ project_name }}-network
    restart: unless-stopped
    profiles:
      - production

  # PostgreSQL database
  postgres:
    image: postgres:15-alpine
    environment:
      - POSTGRES_DB={{ project_name }}
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
      - "5432:5432"
    networks:
      - {{ project_name }}-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis cache
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    networks:
      - {{ project_name }}-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # pgAdmin for database management (development only)
  pgadmin:
    image: dpage/pgadmin4:latest
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin@{{ project_name }}.local
      - PGADMIN_DEFAULT_PASSWORD=admin
    ports:
      - "5050:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    depends_on:
      - postgres
    networks:
      - {{ project_name }}-network
    restart: unless-stopped
    profiles:
      - development

volumes:
  postgres_data:
  redis_data:
  pgadmin_data:

networks:
  {{ project_name }}-network:
    driver: bridge
