services:
  db:
    image: postgres:17 # Use a specific PostgreSQL version
    container_name: postgres_db
    environment:
      POSTGRES_USER: xxx # Define DB user
      POSTGRES_PASSWORD: xxx # Define DB password
      POSTGRES_DB: xxx # 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=xxx
      - GEMINI_API_KEY=xxx
      - SECRET_KEY=xxx
      # 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

  frontend:
    container_name: vite_frontend
    build:
      context: ./fe
      dockerfile: Dockerfile
    ports:
      - "80:80"
    depends_on:
      - backend
    restart: unless-stopped

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