services:
    backend:
        build:
            context: ./backend
            dockerfile: Dockerfile
        container_name: baron_backend
        env_file:
            - .env
        environment:
            - APP_ENV=${APP_ENV:-development}
            - GO_ENV=${APP_ENV:-development}
            - COOKIE_SECRET=${COOKIE_SECRET}
            - JWT_SECRET=${JWT_SECRET}
            - DESCOPE_PROJECT_ID=${DESCOPE_PROJECT_ID}
            - DESCOPE_MANAGEMENT_KEY=${DESCOPE_MANAGEMENT_KEY}
            - NAVER_CLOUD_ACCESS_KEY=${NAVER_CLOUD_ACCESS_KEY}
            - NAVER_CLOUD_SECRET_KEY=${NAVER_CLOUD_SECRET_KEY}
            - NAVER_CLOUD_SERVICE_ID=${NAVER_CLOUD_SERVICE_ID}
            - NAVER_SENDER_PHONE_NUMBER=${NAVER_SENDER_PHONE_NUMBER}
            - USERFRONT_URL=${USERFRONT_URL}
            - IDP_PROVIDER=${IDP_PROVIDER:-ory,descope}
            - KRATOS_ADMIN_URL=${KRATOS_ADMIN_URL:-http://kratos:4434}
            - HYDRA_ADMIN_URL=${HYDRA_ADMIN_URL:-http://hydra:4445}
            - HYDRA_PUBLIC_URL=${HYDRA_PUBLIC_URL:-http://hydra:4444}
            - DB_HOST=postgres
            - CLICKHOUSE_HOST=clickhouse
            - CLICKHOUSE_PORT=${CLICKHOUSE_PORT_NATIVE:-9000}
            - CLICKHOUSE_USER=${CLICKHOUSE_USER:-baron}
            - CLICKHOUSE_PASSWORD=${CLICKHOUSE_PASSWORD:-password}
        ports:
            - "${BACKEND_PORT:-3000}:3000"
        depends_on:
            - infra_check
        networks:
            - baron_net
            - ory-net
        volumes:
            - ./backend:/app
        command: ["go", "run", "./cmd/server/main.go"]

        healthcheck:
            test: ["CMD", "wget", "-qO-", "http://127.0.0.1:3000/health"]
            interval: 10s
            timeout: 5s
            retries: 3
            start_period: 10s

    adminfront:
        build:
            context: ./adminfront
            dockerfile: Dockerfile
        container_name: baron_adminfront
        env_file:
            - .env
        environment:
            - APP_ENV=${APP_ENV:-development}
            - API_PROXY_TARGET=http://baron_backend:3000
        ports:
            - "${ADMIN_PORT:-5173}:5173"
        volumes:
            - ./adminfront:/app
            - /app/node_modules
        networks:
            - baron_net

    devfront:
        build:
            context: ./devfront
            dockerfile: Dockerfile
        container_name: baron_devfront
        env_file:
            - .env
        environment:
            - APP_ENV=${APP_ENV:-development}
            - API_PROXY_TARGET=http://baron_backend:3000
        ports:
            - "${DEVFRONT_PORT:-5174}:5173"
        volumes:
            - ./devfront:/app
            - /app/node_modules
        networks:
            - baron_net

    userfront:
        build:
            context: ./userfront
            dockerfile: Dockerfile
        container_name: baron_userfront
        env_file:
            - .env
        environment:
            - DESCOPE_PROJECT_ID=${DESCOPE_PROJECT_ID}
            - BACKEND_URL=${BACKEND_URL}
            - USERFRONT_URL=${USERFRONT_URL}
            - APP_ENV=${APP_ENV}
        ports:
            - "${USERFRONT_PORT:-5000}:5000"
        networks:
            - baron_net
        depends_on:
            backend:
                condition: service_healthy
        command: >
            /bin/sh -c "mkdir -p /usr/share/nginx/html/assets &&
                        echo \"DESCOPE_PROJECT_ID=$${DESCOPE_PROJECT_ID}\" > /usr/share/nginx/html/assets/.env &&
                        echo \"BACKEND_URL=$${BACKEND_URL}\" >> /usr/share/nginx/html/assets/.env &&
                        echo \"USERFRONT_URL=$${USERFRONT_URL}\" >> /usr/share/nginx/html/assets/.env &&
                        echo \"APP_ENV=$${APP_ENV}\" >> /usr/share/nginx/html/assets/.env &&
                        cp /usr/share/nginx/html/assets/.env /usr/share/nginx/html/.env &&
                        nginx -g 'daemon off;'"

    # Dummy service to wait for infra network if needed,
    # but essentially we assume infra is running.
    # In a real unified stack, we might include infra here or use external links.
    # Here we attach to the same network.
    infra_check:
        image: alpine
        command: ["echo", "Infrastructure assumed running"]
        networks:
            - baron_net

networks:
    baron_net:
        external: true
        name: baron_net
    ory-net:
        external: true
        name: ory-net
