#!/bin/bash
set -e

APP_NAME="Smart Password Manager CLI"
APP_ID="smart-password-manager-cli"
BIN_NAME="smart-password-manager-cli"
REPO_URL="https://github.com/smartlegionlab/smart-password-manager-cli-rs.git"
BIN_DIR="$HOME/.local/bin"

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m'

info()  { echo -e "${GREEN}==>${NC} $*"; }
warn()  { echo -e "${YELLOW}==>${NC} $*"; }
error() { echo -e "${RED}==>${NC} $*" >&2; }
step()  { echo -e "${BOLD}${BLUE}[*]${NC} $*"; }

banner() {
    echo ""
    echo -e "${BOLD}${GREEN}==============================================${NC}"
    echo -e "${BOLD}${GREEN}  Smart Password Manager CLI - Installer${NC}"
    echo -e "${BOLD}${GREEN}==============================================${NC}"
    echo ""
}

banner

echo "This installer will:"
echo "  1. Download the application source code"
echo "  2. Build it with cargo (requires Rust toolchain)"
echo "  3. Install the binary to:  $BIN_DIR/$BIN_NAME"
echo ""
echo "Your password metadata will be stored in:"
echo "  ~/.config/smart_password_manager/passwords.json"
echo ""

step "Checking cargo..."
if ! command -v cargo >/dev/null 2>&1; then
    error "cargo is required but not installed."
    error "Install Rust from https://rustup.rs and try again."
    exit 1
fi
info "Found: $(cargo --version)"

step "Checking git..."
if ! command -v git >/dev/null 2>&1; then
    error "git is required but not installed."
    exit 1
fi
info "git found."

step "Cloning repository..."
TMP_CLONE="$(mktemp -d)"
git clone --depth 1 "$REPO_URL" "$TMP_CLONE" >/dev/null 2>&1
info "Repository cloned."

step "Building release binary..."
pushd "$TMP_CLONE" >/dev/null
cargo build --release >/dev/null 2>&1
popd >/dev/null
info "Build finished."

step "Installing binary..."
mkdir -p "$BIN_DIR"
install -m 755 "$TMP_CLONE/target/release/$BIN_NAME" "$BIN_DIR/$BIN_NAME"
info "Binary installed: $BIN_DIR/$BIN_NAME"

rm -rf "$TMP_CLONE"
info "Temporary clone removed."

echo ""
echo -e "${BOLD}${GREEN}  Installation complete${NC}"
echo ""
echo "  Binary:    $BIN_DIR/$BIN_NAME"
echo "  Metadata:  ~/.config/smart_password_manager/passwords.json"
echo ""
echo "  Launch:"
echo "    $BIN_NAME --help"
echo ""
echo "  If '$BIN_DIR' is not in your PATH:"
echo "    export PATH=\"\$HOME/.local/bin:\$PATH\""
echo ""
echo "  Uninstall:"
echo "    rm -f $BIN_DIR/$BIN_NAME"
echo ""
