#!/bin/bash
# Agentic Engineering Course Installer (Linux/Mac)
# Installs skills, extensions, and configs for Claude Code, Pi Agent, OpenCode
# Usage: bash install.sh [kit-name]
#   kit-name: security, orchestration, verifier, task, research, observability, board, all

set -e

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

TAC_ROOT="$(cd "$(dirname "$0")" && pwd)"
SKILLS_DIR="$TAC_ROOT/skills"
KIT_NAME="${1:-all}"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} Agentic Engineering Course Installer${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Check prerequisites
echo -e "${YELLOW}Checking prerequisites...${NC}"
has_claude=false
has_pi=false

if command -v claude &> /dev/null; then
    has_claude=true
    echo -e "  ${GREEN}[OK] Claude Code found${NC}"
else
    echo -e "  ${YELLOW}[--] Claude Code not installed${NC}"
fi

if command -v pi &> /dev/null; then
    has_pi=true
    echo -e "  ${GREEN}[OK] Pi Agent found${NC}"
else
    echo -e "  ${YELLOW}[--] Pi Agent not installed${NC}"
fi
echo ""

install_kit() {
    local kit_path="$1"
    local kit_name=$(basename "$kit_path")
    
    echo -e "${YELLOW}Installing kit: $kit_name${NC}"
    
    # Install skills
    if [ -d "$kit_path/skills" ]; then
        for skill_dir in "$kit_path/skills"/*/; do
            [ -d "$skill_dir" ] || continue
            local skill_name=$(basename "$skill_dir")
            
            # Claude Code
            if $has_claude; then
                mkdir -p "$HOME/.claude/skills/$skill_name"
                cp -r "$skill_dir"/* "$HOME/.claude/skills/$skill_name/"
                echo -e "  ${GREEN}skill -> ~/.claude/skills/$skill_name/${NC}"
            fi
            
            # Pi Agent
            if $has_pi; then
                mkdir -p "$HOME/.pi/skills/$skill_name"
                cp -r "$skill_dir"/* "$HOME/.pi/skills/$skill_name/"
                echo -e "  ${GREEN}skill -> ~/.pi/skills/$skill_name/${NC}"
            fi
        done
    fi
    
    # Install extensions
    if [ -d "$kit_path/extensions" ]; then
        if $has_pi; then
            mkdir -p "$HOME/.pi/extensions"
            cp "$kit_path"/extensions/*.ts "$HOME/.pi/extensions/" 2>/dev/null || true
            echo -e "  ${GREEN}extensions -> ~/.pi/extensions/${NC}"
        fi
    fi
    
    echo -e "${GREEN}  Done: $kit_name${NC}"
    echo ""
}

# Install requested kit(s)
if [ "$KIT_NAME" = "all" ]; then
    for kit in "$SKILLS_DIR/kits"/*/; do
        [ -d "$kit" ] || continue
        install_kit "$kit"
    done
elif [ -d "$SKILLS_DIR/kits/$KIT_NAME" ]; then
    install_kit "$SKILLS_DIR/kits/$KIT_NAME"
else
    echo -e "${RED}Error: Kit '$KIT_NAME' not found${NC}"
    echo "Available kits:"
    for kit in "$SKILLS_DIR/kits"/*/; do
        echo "  $(basename "$kit")"
    done
    exit 1
fi

# Install course docs
echo -e "${YELLOW}Installing course materials...${NC}"
COURSE_DIR="$HOME/.agentic-engineering-course"
mkdir -p "$COURSE_DIR"
cp "$TAC_ROOT/course/"*.md "$COURSE_DIR/" 2>/dev/null
cp -r "$TAC_ROOT/course/labs" "$COURSE_DIR/" 2>/dev/null
echo -e "  ${GREEN}docs -> $COURSE_DIR/${NC}"
echo ""

echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN} Installation Complete!${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo -e "Course docs:    ${YELLOW}$COURSE_DIR/${NC}"
echo -e "Claude skills:  ${YELLOW}$HOME/.claude/skills/${NC}"
echo -e "Pi skills:      ${YELLOW}$HOME/.pi/skills/${NC}"
echo -e "Pi extensions:  ${YELLOW}$HOME/.pi/extensions/${NC}"
echo ""
echo -e "Quick start:"
echo -e "  ${BLUE}cd $COURSE_DIR${NC}"
echo -e "  ${BLUE}open 00-CURRICULUM.md${NC}"
