@echo off
setlocal enabledelayedexpansion

if "%~1"=="" (
  echo Usage: new-client-compose.bat NAME [PORT]
  exit /b 1
)

set NAME=%~1
set PORT=%~2

:: Normalize name (replace spaces with underscore)
set SLUG=%NAME: =_%

if "%PORT%"=="" (
  for /f %%A in ('dir /b docker-compose-*.yml ^| find /c /v ""') do set COUNT=%%A
  set /a PORT=10001+%COUNT%+1
)

set FILE=docker-compose-%SLUG%.yml

(
  echo version: "3.8"
  echo services:
  echo   db:
  echo     image: postgres:15
  echo     container_name: odoo_%SLUG%_db
  echo     environment:
  echo       POSTGRES_DB: postgres
  echo       POSTGRES_USER: odoo
  echo       POSTGRES_PASSWORD: odoo
  echo     volumes:
  echo       - %SLUG%_pgdata:/var/lib/postgresql/data
  echo     restart: always
  echo.
  echo   odoo:
  echo     image: odoo:17.0
  echo     container_name: odoo_%SLUG%
  echo     depends_on:
  echo       - db
  echo     ports:
  echo       - "%PORT%:8069"
  echo     environment:
  echo       HOST: db
  echo       USER: odoo
  echo       PASSWORD: odoo
  echo     volumes:
  echo       - %SLUG%_odoo_data:/var/lib/odoo
  echo       - ./addons:/mnt/extra-addons
  echo     restart: always
  echo.
  echo volumes:
  echo   %SLUG%_pgdata:
  echo   %SLUG%_odoo_data:
) > "%FILE%"

echo Created %FILE% with Odoo port %PORT%
echo Run: docker-compose -p %SLUG% -f %FILE% up -d
endlocal
