#!/bin/bash

# Set variables
IMAGE_NAME="nise-frontend"
IMAGE_REGISTRY="git.nise.moe/nuff"
IMAGE_VERSION="latest"

rm -rf target/

# Clean and build Angular project
source /usr/share/nvm/init-nvm.sh
nvm use 22.2.0
ng build --configuration production --aot --output-hashing=all
if [ $? -ne 0 ]; then
    echo "ng build failed"
    exit 1
fi

# Build and push Docker image
docker build . -t $IMAGE_NAME:$IMAGE_VERSION
if [ $? -ne 0 ]; then
    echo "Docker build failed"
    exit 1
fi

docker tag $IMAGE_NAME:$IMAGE_VERSION $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION
docker push $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION
if [ "$?" != "0" ]; then
    echo "Error: Failed to push $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION"
    exit 1
fi

echo "Docker image pushed successfully to $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION"
