services:

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    hostname: ${FRONTEND_HOST}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://${FRONTEND_HOST}:3000/health"]
      interval: 10s
      timeout: 5s
      retries: 5

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    environment:
      - DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:5432/${DB_DB}
      - JWT_SECRET=${JWT_SECRET}
      - JWT_EXPIRATION_ACCESS=${JWT_EXPIRATION_ACCESS}
      - JWT_EXPIRATION_REFRESH=${JWT_EXPIRATION_REFRESH}
      - JWT_ISSUER=${JWT_ISSUER}
      - JWT_AUDIENCE=${JWT_AUDIENCE}
    networks:
      - internal_network
    depends_on:
      db:
        condition: service_healthy
    hostname: ${BACKEND_HOST}
    links:
      - db:database
    healthcheck:
      test: ["CMD", "curl", "-f", "http://${BACKEND_HOST}:3000/health"]
      interval: 10s
      timeout: 5s
      retries: 5

  db:
    image: postgres:latest
    restart: always
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: ${DB_DB}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    hostname: ${DB_HOST}
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "user"]
      interval: 1s
      timeout: 5s
      retries: 20

volumes:
  postgres_data:
networks:
  internal_network:
    driver: bridge
