# docker-compose.yml (in project root)
version: '3.8'

services:
  db:
    image: postgres:15 # Use a specific PostgreSQL version
    container_name: postgres_db
    environment:
      POSTGRES_USER: dev_user             # Define DB user
      POSTGRES_PASSWORD: dev_password     # Define DB password
      POSTGRES_DB: dev_db                 # Define Database name
    volumes:
      - postgres_data:/var/lib/postgresql/data # Persist data using a named volume
    ports:
      - "5432:5432" # Expose PostgreSQL port to host (optional, for direct access)
    healthcheck:
        test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
        interval: 10s
        timeout: 5s
        retries: 5
        start_period: 10s
    restart: unless-stopped

  backend:
    container_name: fastapi_backend
    build:
      context: ./be # Path to the directory containing the Dockerfile
      dockerfile: Dockerfile
    volumes:
      # Mount local code into the container for development hot-reloading
      # The code inside the container at /app will mirror your local ./be directory
      - ./be:/app
    ports:
      - "8000:8000" # Map container port 8000 to host port 8000
    environment:
      # Pass the database URL to the backend container
      # Uses the service name 'db' as the host, and credentials defined above
      # IMPORTANT: Use the correct async driver prefix if your app needs it!
      - DATABASE_URL=postgresql+asyncpg://dev_user:dev_password@db:5432/dev_db
      # Add other environment variables needed by the backend here
      # - SOME_OTHER_VAR=some_value
    depends_on:
      db: # Wait for the db service to be healthy before starting backend
        condition: service_healthy
    command: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"] # Override CMD for development reload
    restart: unless-stopped

  pgadmin: # Optional service for database administration
    image: dpage/pgadmin4:latest
    container_name: pgadmin4_server
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@example.com # Change as needed
      PGADMIN_DEFAULT_PASSWORD: admin_password # Change to a secure password
      PGADMIN_CONFIG_SERVER_MODE: 'False' # Run in Desktop mode for easier local dev server setup
    volumes:
      - pgadmin_data:/var/lib/pgadmin # Persist pgAdmin configuration
    ports:
      - "5050:80" # Map container port 80 to host port 5050
    depends_on:
      - db # Depends on the database service
    restart: unless-stopped

volumes: # Define named volumes for data persistence
  postgres_data:
  pgadmin_data: