services:
  mssql:
    image: mcr.microsoft.com/mssql/server:2022-latest
    container_name: bimai-mssql
    hostname: bimai-mssql
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=BimAI_Dev_Pass_2024!
      - MSSQL_PID=Developer
    ports:
      - "1433:1433"
    volumes:
      - mssql-data:/var/opt/mssql
    networks:
      - bimai-network
    healthcheck:
      test: /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "BimAI_Dev_Pass_2024!" -C -Q "SELECT 1" || exit 1
      interval: 10s
      timeout: 3s
      retries: 10
      start_period: 10s

  init-mssql:
    image: mcr.microsoft.com/mssql/server:2022-latest
    container_name: bimai-init-mssql
    restart: "no"
    networks:
      - bimai-network
    depends_on:
      mssql:
        condition: service_healthy
    environment:
      - MSSQL_SA_PASSWORD=BimAI_Dev_Pass_2024!
    entrypoint: >-
      bash -lc "
        set -e
        for i in {1..30}; do
          /opt/mssql-tools18/bin/sqlcmd -S bimai-mssql -U sa -P \"$$MSSQL_SA_PASSWORD\" -C -Q \"SELECT 1\" && break || sleep 2;
        done;
        /opt/mssql-tools18/bin/sqlcmd -S bimai-mssql -U sa -P \"$$MSSQL_SA_PASSWORD\" -C -Q \"IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'bimai') CREATE DATABASE [bimai];\"
      "

networks:
  bimai-network:
    driver: bridge

volumes:
  mssql-data:
    name: bimai-mssql-data
