#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$ROOT_DIR"

echo "==> build hermes"
docker build -t localhost:32000/hermes-mcp:latest -f Dockerfile .

echo "==> build site"
docker build --no-cache -t localhost:32000/squaremcp-site:latest -f product/site/Dockerfile .

echo "==> push hermes"
docker push localhost:32000/hermes-mcp:latest

echo "==> push site"
docker push localhost:32000/squaremcp-site:latest

echo "==> apply ingress"
microk8s kubectl apply -f product/site/squaremcp-k8s-ingress.yaml

echo "==> restart hermes"
microk8s kubectl rollout restart deployment/hermes-mcp -n fetcherpay

echo "==> restart site"
microk8s kubectl rollout restart deployment/squaremcp-site -n fetcherpay

echo "==> wait hermes"
microk8s kubectl rollout status deployment/hermes-mcp -n fetcherpay

echo "==> wait site"
microk8s kubectl rollout status deployment/squaremcp-site -n fetcherpay

echo "==> verify"
npm run test:product-site:verify
