#!/bin/bash
# VibeSOP Installation Script
#
# This script installs VibeSOP configuration for supported platforms.
#
# Usage:
#   ./vibe-install [platform]
#   ./vibe-install --list
#   ./vibe-install --verify [platform]
#
# Examples:
#   ./vibe-install claude-code
#   ./vibe-install opencode
#   ./vibe-install --list

set -e

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

# Print functions
print_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

# Check if Python is available
check_python() {
    if ! command -v python3 &> /dev/null; then
        print_error "Python 3 is required but not installed"
        exit 1
    fi

    # Check Python version
    PYTHON_VERSION=$(python3 --version | awk '{print $2}')
    print_info "Python version: $PYTHON_VERSION"
}

# List supported platforms
list_platforms() {
    print_info "Supported platforms:"

    python3 -c "
from vibesop.installer import VibeSOPInstaller
installer = VibeSOPInstaller()
platforms = installer.list_platforms()
for p in platforms:
    print(f\"  • {p['name']}: {p['description']}\")
    print(f\"    Config dir: {p['config_dir']}\")
" 2>/dev/null || print_warning "Failed to list platforms (vibesop not installed)"
}

# Install VibeSOP for a platform
install_platform() {
    local platform=$1
    local force=$2

    print_info "Installing VibeSOP for platform: $platform"

    python3 -c "
from vibesop.installer import VibeSOPInstaller
installer = VibeSOPInstaller()
result = installer.install('$platform', force=$force)

if result['success']:
    print('✅ Installation successful')
    if result.get('files_created'):
        print(f'Files created: {len(result[\"files_created\"])}')
    if result.get('hooks_installed'):
        print(f'Hooks installed: {len(result[\"hooks_installed\"])}')
else:
    print('❌ Installation failed')
    for error in result.get('errors', []):
        print(f'  Error: {error}')
    exit(1)
" || exit 1
}

# Verify installation
verify_platform() {
    local platform=$1

    print_info "Verifying installation for: $platform"

    python3 -c "
from vibesop.installer import VibeSOPInstaller
installer = VibeSOPInstaller()
result = installer.verify('$platform')

if result['installed']:
    print('✅ Installation verified')
    print(f'Config valid: {result[\"config_valid\"]}')

    hooks = result.get('hooks_installed', {})
    if hooks:
        installed = sum(1 for v in hooks.values() if v)
        total = len(hooks)
        print(f'Hooks: {installed}/{total} installed')
else:
    print('❌ Not installed')
    for issue in result.get('issues', []):
        print(f'  Issue: {issue}')
    exit(1)
" || exit 1
}

# Main script
main() {
    check_python

    # Parse arguments
    PLATFORM=""
    ACTION="install"
    FORCE=false

    while [[ $# -gt 0 ]]; do
        case $1 in
            --list)
                ACTION="list"
                shift
                ;;
            --verify)
                ACTION="verify"
                PLATFORM="$2"
                shift 2
                ;;
            --force)
                FORCE=true
                shift
                ;;
            -h|--help)
                echo "VibeSOP Installation Script"
                echo ""
                echo "Usage:"
                echo "  $0 [platform]           Install VibeSOP for platform"
                echo "  $0 --list               List supported platforms"
                echo "  $0 --verify [platform]  Verify installation"
                echo "  $0 --force              Force overwrite existing config"
                echo ""
                echo "Examples:"
                echo "  $0 claude-code"
                echo "  $0 opencode"
                echo "  $0 --list"
                echo "  $0 --verify claude-code"
                exit 0
                ;;
            *)
                PLATFORM="$1"
                shift
                ;;
        esac
    done

    # Execute action
    case $ACTION in
        list)
            list_platforms
            ;;
        verify)
            if [ -z "$PLATFORM" ]; then
                print_error "Platform required for verify. Use: $0 --verify [platform]"
                exit 1
            fi
            verify_platform "$PLATFORM"
            ;;
        install)
            if [ -z "$PLATFORM" ]; then
                print_error "Platform required. Use: $0 [platform]"
                print_info "Run '$0 --list' to see supported platforms"
                exit 1
            fi
            install_platform "$PLATFORM" "$FORCE"
            ;;
    esac
}

main "$@"
