#!/bin/bash
# Design Phase Workflow Runner
# Usage: ./workflow.sh <product-name> <action>

set -e

PRODUCT="${1:?Product name required}"
ACTION="${2:?Action required: status|start|approve-branding|generate-samples|approve-samples|generate-all|approve-all|set-figma|complete}"

PROJECT_DIR="/home/aria/projects/$PRODUCT"
WORKFLOW="$PROJECT_DIR/agency/artifacts/design/workflow.json"
SCREENS_DIR="$PROJECT_DIR/agency/artifacts/design/screens"
NANO_DIR="/home/aria/.nvm/versions/node/v24.13.0/lib/node_modules/openclaw/skills/nano-banana-pro"

# Ensure directories exist
mkdir -p "$SCREENS_DIR"

# Helper: Get current state
get_state() {
    jq -r '.state' "$WORKFLOW"
}

# Helper: Set state
set_state() {
    local new_state="$1"
    local timestamp=$(date -Iseconds)
    jq --arg state "$new_state" --arg time "$timestamp" \
        '.state = $state | .history += [{"state": $state, "at": $time}]' \
        "$WORKFLOW" > "$WORKFLOW.tmp" && mv "$WORKFLOW.tmp" "$WORKFLOW"
    echo "State changed to: $new_state"
}

# Helper: Update milestone
update_milestone() {
    local milestone_id="$1"
    local status="$2"
    local approved_by="${3:-}"
    local timestamp=$(date -Iseconds)
    
    jq --arg id "$milestone_id" --arg status "$status" --arg by "$approved_by" --arg at "$timestamp" \
        '(.milestones[] | select(.id == $id)) |= . + {"status": $status, "approved_by": $by, "approved_at": $at}' \
        "$WORKFLOW" > "$WORKFLOW.tmp" && mv "$WORKFLOW.tmp" "$WORKFLOW"
}

case "$ACTION" in
    status)
        echo "=== Design Workflow Status ==="
        echo "Product: $PRODUCT"
        echo "State: $(get_state)"
        echo ""
        echo "Milestones:"
        jq -r '.milestones[] | "  [\(.status)] \(.name)"' "$WORKFLOW"
        echo ""
        echo "Screens:"
        jq -r '.screens | "  Total: \(.total_from_prd) | Generated: \(.generated | length) | Approved: \(.approved | length)"' "$WORKFLOW"
        ;;
    
    start)
        if [ "$(get_state)" != "not_started" ]; then
            echo "Error: Design already started (state: $(get_state))"
            exit 1
        fi
        set_state "branding_draft"
        echo "Design phase started. Generate branding next."
        ;;
    
    approve-branding)
        local approver="${3:?Approver name required}"
        set_state "branding_approved"
        update_milestone "branding_approved" "done" "$approver"
        jq --arg by "$approver" --arg at "$(date -Iseconds)" \
            '.branding.status = "approved" | .branding.approved_by = $by | .branding.approved_at = $at' \
            "$WORKFLOW" > "$WORKFLOW.tmp" && mv "$WORKFLOW.tmp" "$WORKFLOW"
        echo "Branding approved by $approver. Ready for sample screens."
        ;;
    
    generate-samples)
        if [ "$(get_state)" != "branding_approved" ]; then
            echo "Error: Branding must be approved first (state: $(get_state))"
            exit 1
        fi
        set_state "sample_screens_generating"
        echo "Ready to generate sample screens. Use Nano Banana Pro."
        ;;
    
    approve-samples)
        local approver="${3:?Approver name required}"
        set_state "sample_screens_approved"
        update_milestone "sample_screens_approved" "done" "$approver"
        echo "Sample screens approved by $approver. Ready to generate all screens."
        ;;
    
    generate-all)
        if [ "$(get_state)" != "sample_screens_approved" ]; then
            echo "Error: Sample screens must be approved first"
            exit 1
        fi
        set_state "all_screens_generating"
        echo "Ready to generate all screens."
        ;;
    
    approve-all)
        local approver="${3:?Approver name required}"
        set_state "all_screens_approved"
        update_milestone "all_screens_approved" "done" "$approver"
        echo "All screens approved by $approver. Ready to create Figma file."
        ;;
    
    set-figma)
        local figma_url="${3:?Figma URL required}"
        set_state "figma_ready"
        update_milestone "figma_ready" "done"
        jq --arg url "$figma_url" --arg at "$(date -Iseconds)" \
            '.figma.status = "ready" | .figma.file_url = $url | .figma.created_at = $at' \
            "$WORKFLOW" > "$WORKFLOW.tmp" && mv "$WORKFLOW.tmp" "$WORKFLOW"
        echo "Figma URL set: $figma_url"
        ;;
    
    complete)
        local approver="${3:?Approver name required}"
        set_state "completed"
        update_milestone "design_complete" "done" "$approver"
        echo "Design phase completed by $approver. Ready for Build phase!"
        ;;
    
    *)
        echo "Unknown action: $ACTION"
        echo "Valid actions: status|start|approve-branding|generate-samples|approve-samples|generate-all|approve-all|set-figma|complete"
        exit 1
        ;;
esac
