#!/bin/bash

# Minimal Workspace CLI - Simplified Command Interface
# Usage: ./workspace-minimal [command] [args]

set -e

# Basic configuration
VERSION="2.0.0"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

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

# Helper functions
header() { echo -e "${BLUE}🚀 Morphism Workspace v$VERSION${NC}"; }
success() { echo -e "${GREEN}✅ $1${NC}"; }
error() { echo -e "${RED}❌ $1${NC}"; exit 1; }
info() { echo -e "${YELLOW}ℹ️  $1${NC}"; }

# Core functions
new_project() {
    local name=$1 template=${2:-"saas-template"}
    [ -z "$name" ] && error "Project name required"
    
    header
    info "Creating project: $name"
    
    if [ -f "scripts/setup-from-template.sh" ]; then
        bash scripts/setup-from-template.sh "$name" "$template"
        success "Project '$name' created!"
        info "Next: cd morphism-ship/lab/$name && npm install"
    else
        error "Setup script not found"
    fi
}

list_projects() {
    header
    info "Project List"
    [ -f "scripts/workspace-dashboard.sh" ] && bash scripts/workspace-dashboard.sh | grep -A 20 "Repository Collections" || error "Dashboard script not found"
}

validate_workspace() {
    header
    info "Workspace Health Check"
    
    # Check directories
    [ -d "scripts" ] && success "Scripts directory exists" || error "Scripts directory missing"
    [ -d "morphism-ship/lab" ] && success "Projects directory exists" || error "Projects directory missing"
    [ -f "README.md" ] && success "README.md exists" || info "README.md missing"
    
    # Count scripts
    local count=$(find scripts -name "*.sh" -type f 2>/dev/null | wc -l)
    info "Found $count automation scripts"
}

start_api() {
    header
    info "Starting API server"
    
    if [ -f "scripts/workspace-api.sh" ]; then
        bash scripts/workspace-api.sh start
        success "API server started!"
        info "Dashboard: http://localhost:3000"
    else
        error "API script not found"
    fi
}

# Main dispatcher
case ${1:-"help"} in
    "help"|"-h"|"--help")
        header
        echo "Usage: workspace-minimal <command> [args]"
        echo ""
        echo "Commands:"
        echo "  new <name> [template]  - Create new project"
        echo "  list                   - List all projects"
        echo "  validate               - Check workspace health"
        echo "  api                    - Start API server"
        echo "  status                 - Show workspace status"
        echo ""
        echo "Examples:"
        echo "  workspace-minimal new my-app"
        echo "  workspace-minimal list"
        echo "  workspace-minimal validate"
        ;;
        
    "new")
        new_project "$2" "$3"
        ;;
        
    "list")
        list_projects
        ;;
        
    "validate")
        validate_workspace
        ;;
        
    "api")
        start_api
        ;;
        
    "status")
        header
        info "Workspace Status"
        info "Repositories: $(find morphism-ship/lab -maxdepth 1 -type d 2>/dev/null | wc -l)"
        info "Scripts: $(find scripts -name "*.sh" -type f 2>/dev/null | wc -l)"
        info "Templates: $(find templates -maxdepth 1 -type d 2>/dev/null | wc -l)"
        ;;
        
    *)
        error "Unknown command: $1"
        ;;
esac
