#!/bin/bash
set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${YELLOW}==> Building package...${NC}"
rm -rf dist/
uv build

echo -e "${YELLOW}==> Publishing to PyPI...${NC}"
uv publish

echo -e "${YELLOW}==> Installing published package from PyPI...${NC}"
uv pip install htd-silo --force-reinstall

echo -e "${YELLOW}==> Regenerating Homebrew formula...${NC}"
PYTHONWARNINGS="ignore::UserWarning" .venv/bin/poet -f htd-silo > Formula/htd-silo.rb

echo -e "${YELLOW}==> Checking for changes...${NC}"
if [[ -n $(git status --porcelain) ]]; then
    echo -e "${GREEN}Changes detected, committing...${NC}"
    git add .
    git commit -m "Release: update package and formula"
else
    echo -e "${GREEN}No changes to commit${NC}"
fi

echo -e "${YELLOW}==> Pushing to remote main branch...${NC}"
git push origin main

echo -e "${YELLOW}==> Updating Homebrew tap...${NC}"
brew tap --force karolkaminskihtd/htd-silo git@github.com:HTD-Health/silo.git

echo -e "${YELLOW}==> Reinstalling htd-silo...${NC}"
brew reinstall htd-silo

echo -e "${GREEN}==> Release complete!${NC}"
silo --help
