version: '3.8'

services:
  db:
    image: postgres:15-alpine
    container_name: portfolio-db
    restart: always
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-fikri}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-fikri}
      POSTGRES_DB: ${POSTGRES_DB:-fullstack-portfolio}
    ports:
      - "5429:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-fikri} -d ${POSTGRES_DB:-fullstack-portfolio}"]
      interval: 5s
      timeout: 5s
      retries: 5

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: portfolio-app
    restart: always
    ports:
      - "4000:4000"
    environment:
      DATABASE_URL: postgresql://${POSTGRES_USER:-fikri}:${POSTGRES_PASSWORD:-fikri}@db:5432/${POSTGRES_DB:-fullstack-portfolio}?schema=public
      JWT_SECRET: ${JWT_SECRET}
      R2_TOKEN: ${R2_TOKEN}
      R2_ACCESS_KEY: ${R2_ACCESS_KEY}
      R2_SECRET_KEY: ${R2_SECRET_KEY}
      R2_ENDPOINT: ${R2_ENDPOINT}
      R2_BUCKET_NAME: ${R2_BUCKET_NAME}
      IMAGE_URL: ${IMAGE_URL}
    depends_on:
      db:
        condition: service_healthy
    command: >
      sh -c "npx prisma db push && node server.js"

volumes:
  postgres_data:
