#!/bin/bash
set -euo pipefail

KUGETSU_DIR="${KUGETSU_DIR:-$HOME/.kugetsu}"
SESSIONS_DIR="$KUGETSU_DIR/sessions"
INDEX_FILE="$KUGETSU_DIR/index.json"

usage() {
    cat << 'EOF'
kugetsu - OpenCode Session Manager (Issue-Driven)

Usage:
    kugetsu init [--force]                            Initialize base session (requires TTY)
    kugetsu start <issue-ref> <message> [--debug]    Start task for issue (forks base session)
    kugetsu continue <issue-ref> [message] [--debug] Continue existing task for issue
    kugetsu list                                     List all tracked sessions
    kugetsu prune [--force]                          Remove orphaned sessions (keeps base)
    kugetsu destroy <issue-ref> [-y]                  Delete session for issue
    kugetsu destroy --base [-y]                       Delete base session
    kugetsu help                                     Show this help

Issue Ref Format:
    instance/user/repo#number
    Example: github.com/shoko/kugetsu#14

Commands:
    init        Create base session via TUI. Requires terminal access.
                Use --force to reinitialize if base session exists.
    start       Fork new session from base for specific issue.
    continue    Continue work on existing issue session.
    list        Show all sessions (base + forked issues).
    prune       Remove sessions not in index (orphaned from opencode).
                Use --force to skip confirmation.
    destroy     Delete specific issue session or base session.

Options:
    --debug   Show real-time debug output and capture to debug.log

Examples:
    kugetsu init
    kugetsu start github.com/shoko/kugetsu#14 "fix bug"
    kugetsu continue github.com/shoko/kugetsu#14 "add tests"
    kugetsu list
    kugetsu prune
    kugetsu prune --force
    kugetsu destroy github.com/shoko/kugetsu#14
EOF
}

ensure_dirs() {
    mkdir -p "$SESSIONS_DIR"
}

issue_ref_to_filename() {
    local issue_ref="$1"
    echo "$issue_ref" | sed 's/[\/:]/-/g' | sed 's/#/-/'
}

filename_to_issue_ref() {
    local filename="$1"
    local name="${filename%.json}"
    echo "$name" | sed 's/-\([0-9]*\)$/#\1' | sed 's/-/\//g'
}

read_index() {
    if [ -f "$INDEX_FILE" ]; then
        cat "$INDEX_FILE"
    else
        echo '{"base": null, "issues": {}}'
    fi
}

write_index() {
    local base="$1"
    local issues_json="$2"
    local temp_file="$INDEX_FILE.tmp.$$"
    printf '{"base": %s, "issues": %s}\n' "$base" "$issues_json" > "$temp_file"
    mv "$temp_file" "$INDEX_FILE"
}

get_base_session_id() {
    local index=$(read_index)
    echo "$index" | python3 -c "import sys, json; d=json.load(sys.stdin); print(d.get('base') or '')"
}

get_session_for_issue() {
    local issue_ref="$1"
    local index=$(read_index)
    echo "$index" | python3 -c "import sys, json; d=json.load(sys.stdin); print(d['issues'].get('$issue_ref') or '')"
}

set_base_in_index() {
    local base_session_id="$1"
    local issues_json=$(read_index | python3 -c "import sys, json; d=json.load(sys.stdin); print(json.dumps(d['issues']))")
    write_index "\"$base_session_id\"" "$issues_json"
}

add_issue_to_index() {
    local issue_ref="$1"
    local session_file="$2"
    local index=$(read_index)
    local base=$(get_base_session_id)
    local issues=$(echo "$index" | python3 -c "import sys, json; d=json.load(sys.stdin); print(json.dumps(d['issues']))")
    local new_issues=$(echo "$issues" | python3 -c "import sys, json; d=json.load(sys.stdin); d['$issue_ref']='$session_file'; print(json.dumps(d))")
    if [ "$base" = "null" ] || [ -z "$base" ]; then
        write_index "null" "$new_issues"
    else
        write_index "\"$base\"" "$new_issues"
    fi
}

remove_issue_from_index() {
    local issue_ref="$1"
    local index=$(read_index)
    local base=$(get_base_session_id)
    local new_issues=$(echo "$index" | python3 -c "import sys, json; d=json.load(sys.stdin); d['issues'].pop('$issue_ref', None); print(json.dumps(d['issues']))")
    if [ "$base" = "null" ] || [ -z "$base" ]; then
        write_index "null" "$new_issues"
    else
        write_index "\"$base\"" "$new_issues"
    fi
}

validate_issue_ref() {
    local issue_ref="$1"
    if [[ ! "$issue_ref" =~ ^[^/]+/[^/]+/[^#]+#[0-9]+$ ]]; then
        echo "Error: invalid issue ref format" >&2
        echo "Expected: instance/user/repo#number" >&2
        echo "Example: github.com/shoko/kugetsu#14" >&2
        exit 1
    fi
}

check_opencode_session_exists() {
    local session_id="$1"
    opencode session list 2>/dev/null | grep -q "^$session_id"
}

DEBUG_MODE=false

set_debug_mode() {
    DEBUG_MODE=false
    local filtered_args=()
    while [ $# -gt 0 ]; do
        case "$1" in
            --debug)
                DEBUG_MODE=true
                ;;
            *)
                filtered_args+=("$1")
                ;;
        esac
        shift
    done
    echo "${filtered_args[@]}"
}

cmd_init() {
    local force=false

    while [ $# -gt 0 ]; do
        case "$1" in
            --force)
                force=true
                ;;
            *)
                ;;
        esac
        shift
    done

    ensure_dirs

    local existing_base=$(get_base_session_id)
    if [ -n "$existing_base" ] && [ "$existing_base" != "null" ]; then
        if [ "$force" = true ]; then
            echo "Warning: Reinitializing base session (force mode)" >&2
        else
            echo "Error: Base session already exists: $existing_base" >&2
            echo "Use --force to reinitialize" >&2
            exit 1
        fi
    fi

    if ! test -t 0; then
        echo "Error: init requires a terminal (TTY)" >&2
        echo "Please run this command in an interactive shell" >&2
        exit 1
    fi

    echo "Starting TUI to create base session..."
    echo "Press Ctrl+C to cancel or wait for session to be created"
    sleep 2

    opencode

    local session_ids=$(opencode session list 2>/dev/null | grep -E '^ses_' | awk '{print $1}' | tail -1)
    if [ -z "$session_ids" ]; then
        echo "Error: Could not find newly created session" >&2
        exit 1
    fi

    local new_session_id=$(echo "$session_ids" | tail -1)
    local session_file="base.json"

    printf '{"type": "base", "opencode_session_id": "%s", "created_at": "%s", "state": "idle"}\n' \
        "$new_session_id" "$(date -Iseconds)" > "$SESSIONS_DIR/$session_file"

    set_base_in_index "$new_session_id"

    echo "Base session initialized: $new_session_id"
}

cmd_start() {
    local issue_ref=""
    local message=""
    local args=("$@")

    args=$(set_debug_mode "${args[@]}")

    for arg in $args; do
        if [ -z "$issue_ref" ]; then
            issue_ref="$arg"
        elif [ -z "$message" ]; then
            message="$arg"
        fi
    done

    if [ -z "$issue_ref" ] || [ -z "$message" ]; then
        echo "Error: start requires <issue-ref> and <message>" >&2
        exit 1
    fi

    validate_issue_ref "$issue_ref"
    ensure_dirs

    local base_session_id=$(get_base_session_id)
    if [ -z "$base_session_id" ] || [ "$base_session_id" = "null" ]; then
        echo "Error: No base session. Run 'kugetsu init' first." >&2
        exit 1
    fi

    local existing_session=$(get_session_for_issue "$issue_ref")
    if [ -n "$existing_session" ] && [ "$existing_session" != "null" ]; then
        echo "Error: Session for '$issue_ref' already exists" >&2
        echo "Use 'kugetsu continue $issue_ref <message>' instead" >&2
        exit 1
    fi

    local session_file="$(issue_ref_to_filename "$issue_ref").json"

    local before_sessions=$(opencode session list 2>/dev/null | grep -oP '^ses_\w+' | sort)
    local before_set="${before_sessions//$'\n'/|}"

    echo "Forking session for '$issue_ref'..."
    if [ "$DEBUG_MODE" = true ]; then
        opencode run --fork --session "$base_session_id" "$message" 2>&1 | tee "$SESSIONS_DIR/$session_file.debug.log"
    else
        opencode run --fork --session "$base_session_id" "$message" 2>&1
    fi

    local after_sessions=$(opencode session list 2>/dev/null | grep -oP '^ses_\w+' | sort)
    local new_session_id=""
    while IFS= read -r sess; do
        if [[ ! "$before_set" =~ \|${sess}\| ]] && [[ "$sess" != "$base_session_id" ]]; then
            new_session_id="$sess"
            break
        fi
    done <<< "$after_sessions"

    if [ -z "$new_session_id" ]; then
        echo "Error: Could not find newly created session" >&2
        exit 1
    fi

    printf '{"type": "forked", "issue_ref": "%s", "opencode_session_id": "%s", "created_at": "%s", "state": "idle"}\n' \
        "$issue_ref" "$new_session_id" "$(date -Iseconds)" > "$SESSIONS_DIR/$session_file"

    add_issue_to_index "$issue_ref" "$session_file"

    echo "Session started for '$issue_ref': $new_session_id"
}

cmd_continue() {
    local issue_ref=""
    local message=""
    local args=("$@")

    args=$(set_debug_mode "${args[@]}")

    for arg in $args; do
        if [ -z "$issue_ref" ]; then
            issue_ref="$arg"
        elif [ -z "$message" ]; then
            message="$arg"
        fi
    done

    if [ -z "$issue_ref" ]; then
        echo "Error: continue requires <issue-ref>" >&2
        exit 1
    fi

    if [ -z "$message" ]; then
        echo "Error: continue requires <message>" >&2
        exit 1
    fi

    validate_issue_ref "$issue_ref"

    local session_file=$(get_session_for_issue "$issue_ref")
    if [ -z "$session_file" ] || [ "$session_file" = "null" ]; then
        echo "Error: No session found for '$issue_ref'" >&2
        echo "Use 'kugetsu start $issue_ref <message>' to create one" >&2
        exit 1
    fi

    local session_path="$SESSIONS_DIR/$session_file"
    if [ ! -f "$session_path" ]; then
        echo "Error: Session file missing: $session_path" >&2
        echo "Run 'kugetsu start $issue_ref <message>' to recreate" >&2
        exit 1
    fi

    local opencode_session_id=$(python3 -c "import json; print(json.load(open('$session_path'))['opencode_session_id'])")

    if ! check_opencode_session_exists "$opencode_session_id"; then
        echo "Warning: Session may have expired in opencode" >&2
        echo "Attempting to continue anyway..." >&2
    fi

    echo "Continuing session for '$issue_ref'..."
    if [ "$DEBUG_MODE" = true ]; then
        opencode run --continue --session "$opencode_session_id" "$message" 2>&1 | tee "$session_path.debug.log"
    else
        opencode run --continue --session "$opencode_session_id" "$message"
    fi
}

cmd_list() {
    ensure_dirs

    printf "%-50s %-10s %-25s %s\n" "ISSUE_REF" "TYPE" "SESSION_ID" "CREATED"
    printf "%-50s %-10s %-25s %s\n" "─────────" "─────" "──────────" "───────"

    local base_session_id=$(get_base_session_id)
    if [ -n "$base_session_id" ] && [ "$base_session_id" != "null" ]; then
        printf "%-50s %-10s %-25s %s\n" "(base)" "base" "$base_session_id" "N/A"
    fi

    local index=$(read_index)
    local issue_refs=$(echo "$index" | python3 -c "import sys, json; d=json.load(sys.stdin); print('\n'.join(d['issues'].keys()))" 2>/dev/null || true)

    for session_file in "$SESSIONS_DIR"/*.json; do
        if [ -f "$session_file" ]; then
            local filename=$(basename "$session_file" .json)
            if [ "$filename" = "base" ]; then
                continue
            fi

            local issue_ref=$(python3 -c "import json; print(json.load(open('$session_file'))['issue_ref'])" 2>/dev/null || echo "$filename")
            local sess_id=$(python3 -c "import json; print(json.load(open('$session_file'))['opencode_session_id'])" 2>/dev/null || echo "unknown")
            local created=$(python3 -c "import json; print(json.load(open('$session_file'))['created_at'])" 2>/dev/null || echo "unknown")

            printf "%-50s %-10s %-25s %s\n" "$issue_ref" "forked" "$sess_id" "$created"
        fi
    done
}

cmd_prune() {
    local force=false

    while [ $# -gt 0 ]; do
        case "$1" in
            --force)
                force=true
                ;;
        esac
        shift
    done

    ensure_dirs

    local index=$(read_index)
    local index_session_files=$(echo "$index" | python3 -c "import sys, json; d=json.load(sys.stdin); sessions=set(d['issues'].values()); sessions.add('base.json'); print('\n'.join(sessions))" 2>/dev/null || echo "base.json")

    local orphaned=()
    for session_file in "$SESSIONS_DIR"/*.json; do
        if [ -f "$session_file" ]; then
            local filename=$(basename "$session_file")
            if ! echo "$index_session_files" | grep -q "^$filename$"; then
                orphaned+=("$session_file")
            fi
        fi
    done

    if [ ${#orphaned[@]} -eq 0 ]; then
        echo "No orphaned sessions found"
        return
    fi

    echo "Found ${#orphaned[@]} orphaned session(s):"
    for f in "${orphaned[@]}"; do
        echo "  - $(basename "$f")"
    done

    if [ "$force" = true ]; then
        echo "Removing orphaned sessions (force mode)..."
        for f in "${orphaned[@]}"; do
            rm -f "$f"
            echo "Removed: $(basename "$f")"
        done
    else
        echo "Run with --force to remove"
    fi
}

cmd_destroy() {
    local target=""
    local force=false

    while [ $# -gt 0 ]; do
        case "$1" in
            --base)
                target="base"
                ;;
            -y|--yes)
                force=true
                ;;
            *)
                if [ -z "$target" ]; then
                    target="$1"
                fi
                ;;
        esac
        shift
    done

    if [ -z "$target" ]; then
        echo "Error: destroy requires <issue-ref> or --base" >&2
        exit 1
    fi

    if [ "$target" = "base" ]; then
        if [ "$force" = true ]; then
            rm -f "$SESSIONS_DIR/base.json"
            echo '{"base": null, "issues": {}}' > "$INDEX_FILE"
            echo "Base session destroyed"
        else
            echo "Error: destroying base session requires --base -y" >&2
            exit 1
        fi
        return
    fi

    validate_issue_ref "$target"

    local session_file=$(get_session_for_issue "$target")
    if [ -z "$session_file" ] || [ "$session_file" = "null" ]; then
        echo "Error: No session found for '$target'" >&2
        exit 1
    fi

    local session_path="$SESSIONS_DIR/$session_file"

    if [ "$force" = true ]; then
        rm -f "$session_path"
        remove_issue_from_index "$target"
        echo "Session for '$target' destroyed"
    else
        echo "Delete session for '$target'? [y/N] "
        local reply
        read reply
        if [ "$reply" = "y" ] || [ "$reply" = "Y" ]; then
            rm -f "$session_path"
            remove_issue_from_index "$target"
            echo "Session for '$target' destroyed"
        else
            echo "Aborted"
        fi
    fi
}

main() {
    if [ $# -eq 0 ]; then
        usage
        exit 1
    fi

    local command="$1"
    shift

    case "$command" in
        help|--help|-h)
            usage
            ;;
        init)
            cmd_init "$@"
            ;;
        start)
            cmd_start "$@"
            ;;
        continue)
            cmd_continue "$@"
            ;;
        list)
            cmd_list "$@"
            ;;
        prune)
            cmd_prune "$@"
            ;;
        destroy)
            cmd_destroy "$@"
            ;;
        *)
            echo "Error: unknown command '$command'" >&2
            usage
            exit 1
            ;;
    esac
}

main "$@"