#!/bin/bash
set -e

tag=$1
# is_prod=${$2:-False}

echo "[INFO] Tag received from Python: $tag"

# echo "[INFO] Updating everything the remote..."
# git submodule update --recursive --remote

echo "[INFO] Pushing AI image with tag: $tag..."
docker compose -f docker-compose-dev.yml build cope2n-fi-sbt
docker tag sidp/cope2n-ai-fi-sbt:latest public.ecr.aws/sdsrv/sidp/cope2n-ai-fi-sbt:${tag}
docker push public.ecr.aws/sdsrv/sidp/cope2n-ai-fi-sbt:${tag}
# docker tag sidp/cope2n-ai-fi-sbt:latest public.ecr.aws/sdsrv/sidp/cope2n-ai-fi-sbt:production
# docker push public.ecr.aws/sdsrv/sidp/cope2n-ai-fi-sbt:production

echo "[INFO] Pushing BE image with tag: $tag..."
docker compose -f docker-compose-dev.yml build be-ctel-sbt
docker tag sidp/cope2n-be-fi-sbt:latest public.ecr.aws/sdsrv/sidp/cope2n-be-fi-sbt:${tag}
# docker tag sidp/cope2n-be-fi-sbt:latest public.ecr.aws/sdsrv/sidp/cope2n-be-fi-sbt:production
docker push public.ecr.aws/sdsrv/sidp/cope2n-be-fi-sbt:${tag}
# docker push public.ecr.aws/sdsrv/sidp/cope2n-be-fi-sbt:production

echo "[INFO] Pushing FE image with tag: $tag..."
docker compose -f docker-compose-dev.yml build fe-sbt
docker tag sidp/cope2n-fe-fi-sbt:latest public.ecr.aws/sdsrv/sidp/cope2n-fe-fi-sbt:${tag}
# docker tag sidp/cope2n-fe-fi-sbt:latest public.ecr.aws/sdsrv/sidp/cope2n-fe-fi-sbt:production
docker push public.ecr.aws/sdsrv/sidp/cope2n-fe-fi-sbt:${tag}
# docker push public.ecr.aws/sdsrv/sidp/cope2n-fe-fi-sbt:production

echo "[INFO] Pushing API caller image with tag: $tag..."
docker compose -f docker-compose-dev.yml build dashboard_refresh
docker tag sidp/api-caller-sbt:latest public.ecr.aws/sdsrv/sidp/api-caller-sbt:${tag}
docker push public.ecr.aws/sdsrv/sidp/api-caller-sbt:${tag}

cp ./docker-compose-prod.yml ./docker-compose_${tag}.yml
sed -i "s/{{tag}}/$tag/g" ./docker-compose_${tag}.yml
cp .env_prod .env_${tag}