services:
  db:
    image: postgres:17 # 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
      - GEMINI_API_KEY=AIzaSyDKoZBIzUKoeGRtc3m7FtSoqId_nZjfl7M
      - SECRET_KEY=zaSyDKoZBIzUKoeGRtc3m7zaSyGRtc3m7zaSyDKoZBIzUKoeGRtc3m7
      # 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: