version: '3.9'

services:
    # PostgreSQL Database
    postgres:
        image: postgres:17-alpine
        container_name: emailhelper-postgres
        environment:
            POSTGRES_DB: ${DB_NAME:-robocoding}
            POSTGRES_USER: ${DB_USER:-postgres}
            POSTGRES_PASSWORD: ${DB_PASSWORD:-root}
        ports:
            - '${DB_PORT:-5432}:5432'
        volumes:
            - postgres_data:/var/lib/postgresql/data
        networks:
            - emailhelper-network
        healthcheck:
            test: ['CMD-SHELL', 'pg_isready -U ${DB_USER:-postgres}']
            interval: 10s
            timeout: 5s
            retries: 5

    # Node.js Application
    app:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: emailhelper-app
        environment:
            PORT: ${PORT:-9090}
            DB_NAME: ${DB_NAME:-robocoding}
            DB_USER: ${DB_USER:-postgres}
            DB_PASSWORD: ${DB_PASSWORD:-root}
            DB_HOST: postgres
            DB_PORT: 5432
            JWT_SECRET: ${JWT_SECRET}
            DATABASE_URL: postgresql://${DB_USER:-postgres}:${DB_PASSWORD:-root}@postgres:5432/${DB_NAME:-robocoding}
        ports:
            - '${PORT:-9090}:9090'
        depends_on:
            postgres:
                condition: service_healthy
        volumes:
            - ./www.yello.ae_data.json:/usr/src/app/www.yello.ae_data.json
        networks:
            - emailhelper-network
        restart: unless-stopped
        command: sh -c "sleep 10 && npm start"

volumes:
    postgres_data:
        driver: local

networks:
    emailhelper-network:
        driver: bridge
