version: '3.8'

services:
  # Base de datos PostgreSQL
  db:
    image: postgres:16-alpine
    container_name: smashpoint-db
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-padel_user}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-padel_password}
      POSTGRES_DB: ${POSTGRES_DB:-smashpoint_db}
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./scripts/init-db.sql:/docker-entrypoint-initdb.d/init.sql:ro
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-padel_user} -d ${POSTGRES_DB:-smashpoint_db}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - smashpoint-network

  # Aplicacion Web Next.js
  web:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: smashpoint-web
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      NODE_ENV: production
      DATABASE_URL: postgresql://${POSTGRES_USER:-padel_user}:${POSTGRES_PASSWORD:-padel_password}@db:5432/${POSTGRES_DB:-smashpoint_db}?schema=public
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET}
      NEXTAUTH_URL: ${NEXTAUTH_URL:-http://localhost:3000}
      NEXT_PUBLIC_APP_URL: ${NEXT_PUBLIC_APP_URL:-http://localhost:3000}
    ports:
      - "3000:3000"
    volumes:
      - ./apps/web/public:/app/apps/web/public:ro
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/api/auth/session"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    networks:
      - smashpoint-network

networks:
  smashpoint-network:
    driver: bridge

volumes:
  postgres_data:
    driver: local
