services:
  frontend:
    build: ./frontend
    container_name: bookibra_frontend
    restart: unless-stopped
    ports:
      - "5173:5173"
    depends_on:
      api:
        condition: service_started
    volumes:
      - ./frontend:/app
      - frontend-node_modules:/app/node_modules
    command: sh -c "npm install && npm run dev -- --host 0.0.0.0 --port 5173"

  api:
    build: .
    container_name: bookibra_api
    restart: unless-stopped
    env_file:
      - .env
    ports:
      - "8080:8080"
    depends_on:
      redis:
        condition: service_healthy
      postgres:
        condition: service_healthy
    volumes:
      - ./:/app
    command: npm run dev

  redis:
    image: redis:7-alpine
    container_name: bookibra_redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  postgres:
    image: postgres:15-alpine
    container_name: bookibra_postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-bookibra}
      POSTGRES_USER: ${POSTGRES_USER:-bookibra}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-bookibra}
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-bookibra}"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  postgres-data:
  frontend-node_modules:
