#!/usr/bin/env bash
# Nomarchy Pre-flight State Migration
# Migrates legacy state files into the unified state.json before Nix evaluation

STATE_DIR="$HOME/.config/nomarchy"
OLD_STATE_DIR="$HOME/.config/home-manager"
OLD_TOGGLES_DIR="$HOME/.local/state/nomarchy/toggles"
IDLE_STATE_FILE="$OLD_STATE_DIR/idle-state.json"
NIGHTLIGHT_STATE_FILE="$OLD_STATE_DIR/hyprsunset-state.json"
HYPRLAND_STATE_FILE="$OLD_STATE_DIR/hyprland-state.json"
THEME_STATE_FILE="$OLD_STATE_DIR/theme-state.nix"
WALLPAPER_STATE_FILE="$OLD_STATE_DIR/wallpaper-state.nix"
FONT_STATE_FILE="$OLD_STATE_DIR/font-state.nix"
OLD_STATE_FILE="$OLD_STATE_DIR/state.json"
NEW_STATE_FILE="$STATE_DIR/state.json"

# We expect jq to be in PATH (it's a dependency of nomarchy-scripts)
JQ="jq"

mkdir -p "$STATE_DIR"
[[ ! -f $NEW_STATE_FILE ]] && echo "{}" > "$NEW_STATE_FILE"

# 0. Migrate from old home-manager state.json location
if [[ -f "$OLD_STATE_FILE" ]] && [[ "$OLD_STATE_FILE" != "$NEW_STATE_FILE" ]]; then
    # Merge old state into new state
    TMP_FILE=$(mktemp)
    $JQ -s '.[0] * .[1]' "$OLD_STATE_FILE" "$NEW_STATE_FILE" > "$TMP_FILE" && mv "$TMP_FILE" "$NEW_STATE_FILE"
    rm "$OLD_STATE_FILE" 2>/dev/null || true
fi

# 1. Migrate .local/state/nomarchy/toggles
if [[ -d $OLD_TOGGLES_DIR ]]; then
    for file in "$OLD_TOGGLES_DIR"/*; do
        [[ -e "$file" ]] || continue
        filename=$(basename "$file")
        case "$filename" in
            suspend-off)
                $JQ '.suspend = false' "$NEW_STATE_FILE" > "$NEW_STATE_FILE.tmp" && mv "$NEW_STATE_FILE.tmp" "$NEW_STATE_FILE"
                ;;
            screensaver-off)
                $JQ '.screensaver = false' "$NEW_STATE_FILE" > "$NEW_STATE_FILE.tmp" && mv "$NEW_STATE_FILE.tmp" "$NEW_STATE_FILE"
                ;;
            skip-vscode-theme-changes)
                $JQ '.skipVsCodeTheme = true' "$NEW_STATE_FILE" > "$NEW_STATE_FILE.tmp" && mv "$NEW_STATE_FILE.tmp" "$NEW_STATE_FILE"
                ;;
        esac
        rm "$file"
    done
    rmdir "$OLD_TOGGLES_DIR" 2>/dev/null || true
fi

# 2. Migrate existing JSON state files
if [[ -f $IDLE_STATE_FILE ]]; then
    ENABLED=$($JQ '.enabled' "$IDLE_STATE_FILE")
    if [[ "$ENABLED" == "true" || "$ENABLED" == "false" ]]; then
        $JQ --argjson val "$ENABLED" '.idle = $val' "$NEW_STATE_FILE" > "$NEW_STATE_FILE.tmp" && mv "$NEW_STATE_FILE.tmp" "$NEW_STATE_FILE"
    fi
    rm "$IDLE_STATE_FILE"
fi

if [[ -f $NIGHTLIGHT_STATE_FILE ]]; then
    ENABLED=$($JQ '.enabled' "$NIGHTLIGHT_STATE_FILE")
    TEMP=$($JQ '.temperature' "$NIGHTLIGHT_STATE_FILE")
    $JQ --argjson enabled "$ENABLED" --argjson temp "$TEMP" '.nightlight = $enabled | .nightlightTemperature = $temp' "$NEW_STATE_FILE" > "$NEW_STATE_FILE.tmp" && mv "$NEW_STATE_FILE.tmp" "$NEW_STATE_FILE"
    rm "$NIGHTLIGHT_STATE_FILE"
fi

if [[ -f $HYPRLAND_STATE_FILE ]]; then
    GAPS_OUT=$($JQ '.gaps_out' "$HYPRLAND_STATE_FILE")
    GAPS_IN=$($JQ '.gaps_in' "$HYPRLAND_STATE_FILE")
    BORDER_SIZE=$($JQ '.border_size' "$HYPRLAND_STATE_FILE")
    $JQ --argjson go "$GAPS_OUT" --argjson gi "$GAPS_IN" --argjson bs "$BORDER_SIZE" '.hyprland = {"gaps_out": $go, "gaps_in": $gi, "border_size": $bs}' "$NEW_STATE_FILE" > "$NEW_STATE_FILE.tmp" && mv "$NEW_STATE_FILE.tmp" "$NEW_STATE_FILE"
    rm "$HYPRLAND_STATE_FILE"
fi

# 3. Migrate plaintext string state files
if [[ -f $THEME_STATE_FILE ]]; then
    THEME=$(cat "$THEME_STATE_FILE" | tr -d '\n')
    $JQ --arg theme "$THEME" '.theme = $theme' "$NEW_STATE_FILE" > "$NEW_STATE_FILE.tmp" && mv "$NEW_STATE_FILE.tmp" "$NEW_STATE_FILE"
    rm "$THEME_STATE_FILE"
fi

if [[ -f $WALLPAPER_STATE_FILE ]]; then
    WALLPAPER=$(cat "$WALLPAPER_STATE_FILE" | tr -d '\n')
    $JQ --arg wp "$WALLPAPER" '.wallpaper = $wp' "$NEW_STATE_FILE" > "$NEW_STATE_FILE.tmp" && mv "$NEW_STATE_FILE.tmp" "$NEW_STATE_FILE"
    rm "$WALLPAPER_STATE_FILE"
fi

if [[ -f $FONT_STATE_FILE ]]; then
    FONT=$(cat "$FONT_STATE_FILE" | tr -d '\n')
    $JQ --arg font "$FONT" '.font = $font' "$NEW_STATE_FILE" > "$NEW_STATE_FILE.tmp" && mv "$NEW_STATE_FILE.tmp" "$NEW_STATE_FILE"
    rm "$FONT_STATE_FILE"
fi
