#!/bin/bash

# Update all references to renamed terminal managers

set -e

echo "Updating all references to renamed terminal managers..."

# Function to update references in a file
update_file() {
  local file="$1"
  
  # Update module definitions first
  perl -pi -e 's/defmodule Raxol\.Terminal\.Buffer\.Manager/defmodule Raxol.Terminal.Buffer.BufferManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Capabilities\.Manager/defmodule Raxol.Terminal.Capabilities.CapabilitiesManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Charset\.Manager/defmodule Raxol.Terminal.Charset.CharsetManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Clipboard\.Manager/defmodule Raxol.Terminal.Clipboard.ClipboardManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Color\.Manager/defmodule Raxol.Terminal.Color.ColorManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Command\.Manager/defmodule Raxol.Terminal.Command.CommandManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Config\.Manager/defmodule Raxol.Terminal.Config.TerminalConfigManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Cursor\.Manager/defmodule Raxol.Terminal.Cursor.CursorManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Extension\.Manager/defmodule Raxol.Terminal.Extension.ExtensionManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Font\.Manager/defmodule Raxol.Terminal.Font.FontManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Formatting\.Manager/defmodule Raxol.Terminal.Formatting.FormattingManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Graphics\.Manager/defmodule Raxol.Terminal.Graphics.GraphicsManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Hyperlink\.Manager/defmodule Raxol.Terminal.Hyperlink.HyperlinkManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Input\.Manager/defmodule Raxol.Terminal.Input.InputManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Manager(?!\w)/defmodule Raxol.Terminal.TerminalManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Metrics\.Manager/defmodule Raxol.Terminal.Metrics.MetricsManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Mode\.Manager/defmodule Raxol.Terminal.Mode.ModeManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Mouse\.Manager/defmodule Raxol.Terminal.Mouse.MouseManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Output\.Manager/defmodule Raxol.Terminal.Output.OutputManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Parser\.State\.Manager/defmodule Raxol.Terminal.Parser.State.ParserStateManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Plugin\.Manager/defmodule Raxol.Terminal.Plugin.TerminalPluginManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Screen\.Manager/defmodule Raxol.Terminal.Screen.ScreenManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Scroll\.Manager/defmodule Raxol.Terminal.Scroll.ScrollManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Scrollback\.Manager/defmodule Raxol.Terminal.Scrollback.ScrollbackManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Selection\.Manager/defmodule Raxol.Terminal.Selection.SelectionManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Split\.Manager/defmodule Raxol.Terminal.Split.SplitManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.State\.Manager/defmodule Raxol.Terminal.State.StateManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Style\.Manager/defmodule Raxol.Terminal.Style.StyleManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Sync\.Manager/defmodule Raxol.Terminal.Sync.SyncManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Tab\.Manager/defmodule Raxol.Terminal.Tab.TabManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.TerminalState\.Manager/defmodule Raxol.Terminal.TerminalState.TerminalStateManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Theme\.Manager/defmodule Raxol.Terminal.Theme.ThemeManager/g' "$file"
  perl -pi -e 's/defmodule Raxol\.Terminal\.Window\.Manager/defmodule Raxol.Terminal.Window.WindowManager/g' "$file"

  # Update alias statements
  perl -pi -e 's/alias Raxol\.Terminal\.Buffer\.Manager/alias Raxol.Terminal.Buffer.BufferManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Capabilities\.Manager/alias Raxol.Terminal.Capabilities.CapabilitiesManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Charset\.Manager/alias Raxol.Terminal.Charset.CharsetManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Clipboard\.Manager/alias Raxol.Terminal.Clipboard.ClipboardManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Color\.Manager/alias Raxol.Terminal.Color.ColorManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Command\.Manager/alias Raxol.Terminal.Command.CommandManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Config\.Manager/alias Raxol.Terminal.Config.TerminalConfigManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Cursor\.Manager/alias Raxol.Terminal.Cursor.CursorManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Extension\.Manager/alias Raxol.Terminal.Extension.ExtensionManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Font\.Manager/alias Raxol.Terminal.Font.FontManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Formatting\.Manager/alias Raxol.Terminal.Formatting.FormattingManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Graphics\.Manager/alias Raxol.Terminal.Graphics.GraphicsManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Hyperlink\.Manager/alias Raxol.Terminal.Hyperlink.HyperlinkManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Input\.Manager/alias Raxol.Terminal.Input.InputManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Manager(?!\w)/alias Raxol.Terminal.TerminalManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Metrics\.Manager/alias Raxol.Terminal.Metrics.MetricsManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Mode\.Manager/alias Raxol.Terminal.Mode.ModeManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Mouse\.Manager/alias Raxol.Terminal.Mouse.MouseManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Output\.Manager/alias Raxol.Terminal.Output.OutputManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Parser\.State\.Manager/alias Raxol.Terminal.Parser.State.ParserStateManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Plugin\.Manager/alias Raxol.Terminal.Plugin.TerminalPluginManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Screen\.Manager/alias Raxol.Terminal.Screen.ScreenManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Scroll\.Manager/alias Raxol.Terminal.Scroll.ScrollManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Scrollback\.Manager/alias Raxol.Terminal.Scrollback.ScrollbackManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Selection\.Manager/alias Raxol.Terminal.Selection.SelectionManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Split\.Manager/alias Raxol.Terminal.Split.SplitManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.State\.Manager/alias Raxol.Terminal.State.StateManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Style\.Manager/alias Raxol.Terminal.Style.StyleManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Sync\.Manager/alias Raxol.Terminal.Sync.SyncManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Tab\.Manager/alias Raxol.Terminal.Tab.TabManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.TerminalState\.Manager/alias Raxol.Terminal.TerminalState.TerminalStateManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Theme\.Manager/alias Raxol.Terminal.Theme.ThemeManager/g' "$file"
  perl -pi -e 's/alias Raxol\.Terminal\.Window\.Manager/alias Raxol.Terminal.Window.WindowManager/g' "$file"

  # Update references (e.g., Terminal.Buffer.Manager -> Terminal.Buffer.BufferManager)
  perl -pi -e 's/Terminal\.Buffer\.Manager(?!\w)/Terminal.Buffer.BufferManager/g' "$file"
  perl -pi -e 's/Terminal\.Capabilities\.Manager(?!\w)/Terminal.Capabilities.CapabilitiesManager/g' "$file"
  perl -pi -e 's/Terminal\.Charset\.Manager(?!\w)/Terminal.Charset.CharsetManager/g' "$file"
  perl -pi -e 's/Terminal\.Clipboard\.Manager(?!\w)/Terminal.Clipboard.ClipboardManager/g' "$file"
  perl -pi -e 's/Terminal\.Color\.Manager(?!\w)/Terminal.Color.ColorManager/g' "$file"
  perl -pi -e 's/Terminal\.Command\.Manager(?!\w)/Terminal.Command.CommandManager/g' "$file"
  perl -pi -e 's/Terminal\.Config\.Manager(?!\w)/Terminal.Config.TerminalConfigManager/g' "$file"
  perl -pi -e 's/Terminal\.Cursor\.Manager(?!\w)/Terminal.Cursor.CursorManager/g' "$file"
  perl -pi -e 's/Terminal\.Extension\.Manager(?!\w)/Terminal.Extension.ExtensionManager/g' "$file"
  perl -pi -e 's/Terminal\.Font\.Manager(?!\w)/Terminal.Font.FontManager/g' "$file"
  perl -pi -e 's/Terminal\.Formatting\.Manager(?!\w)/Terminal.Formatting.FormattingManager/g' "$file"
  perl -pi -e 's/Terminal\.Graphics\.Manager(?!\w)/Terminal.Graphics.GraphicsManager/g' "$file"
  perl -pi -e 's/Terminal\.Hyperlink\.Manager(?!\w)/Terminal.Hyperlink.HyperlinkManager/g' "$file"
  perl -pi -e 's/Terminal\.Input\.Manager(?!\w)/Terminal.Input.InputManager/g' "$file"
  perl -pi -e 's/(?<!\.Buffer\.)(?<!\.Capabilities\.)(?<!\.Charset\.)(?<!\.Clipboard\.)(?<!\.Color\.)(?<!\.Command\.)(?<!\.Config\.)(?<!\.Cursor\.)(?<!\.Extension\.)(?<!\.Font\.)(?<!\.Formatting\.)(?<!\.Graphics\.)(?<!\.Hyperlink\.)(?<!\.Input\.)(?<!\.Metrics\.)(?<!\.Mode\.)(?<!\.Mouse\.)(?<!\.Output\.)(?<!\.Parser\.State\.)(?<!\.Plugin\.)(?<!\.Screen\.)(?<!\.Scroll\.)(?<!\.Scrollback\.)(?<!\.Selection\.)(?<!\.Split\.)(?<!\.State\.)(?<!\.Style\.)(?<!\.Sync\.)(?<!\.Tab\.)(?<!\.TerminalState\.)(?<!\.Theme\.)(?<!\.Window\.)Terminal\.Manager(?!\w)/Terminal.TerminalManager/g' "$file"
  perl -pi -e 's/Terminal\.Metrics\.Manager(?!\w)/Terminal.Metrics.MetricsManager/g' "$file"
  perl -pi -e 's/Terminal\.Mode\.Manager(?!\w)/Terminal.Mode.ModeManager/g' "$file"
  perl -pi -e 's/Terminal\.Mouse\.Manager(?!\w)/Terminal.Mouse.MouseManager/g' "$file"
  perl -pi -e 's/Terminal\.Output\.Manager(?!\w)/Terminal.Output.OutputManager/g' "$file"
  perl -pi -e 's/Parser\.State\.Manager(?!\w)/Parser.State.ParserStateManager/g' "$file"
  perl -pi -e 's/Terminal\.Plugin\.Manager(?!\w)/Terminal.Plugin.TerminalPluginManager/g' "$file"
  perl -pi -e 's/Terminal\.Screen\.Manager(?!\w)/Terminal.Screen.ScreenManager/g' "$file"
  perl -pi -e 's/Terminal\.Scroll\.Manager(?!\w)/Terminal.Scroll.ScrollManager/g' "$file"
  perl -pi -e 's/Terminal\.Scrollback\.Manager(?!\w)/Terminal.Scrollback.ScrollbackManager/g' "$file"
  perl -pi -e 's/Terminal\.Selection\.Manager(?!\w)/Terminal.Selection.SelectionManager/g' "$file"
  perl -pi -e 's/Terminal\.Split\.Manager(?!\w)/Terminal.Split.SplitManager/g' "$file"
  perl -pi -e 's/Terminal\.State\.Manager(?!\w)/Terminal.State.StateManager/g' "$file"
  perl -pi -e 's/Terminal\.Style\.Manager(?!\w)/Terminal.Style.StyleManager/g' "$file"
  perl -pi -e 's/Terminal\.Sync\.Manager(?!\w)/Terminal.Sync.SyncManager/g' "$file"
  perl -pi -e 's/Terminal\.Tab\.Manager(?!\w)/Terminal.Tab.TabManager/g' "$file"
  perl -pi -e 's/Terminal\.TerminalState\.Manager(?!\w)/Terminal.TerminalState.TerminalStateManager/g' "$file"
  perl -pi -e 's/Terminal\.Theme\.Manager(?!\w)/Terminal.Theme.ThemeManager/g' "$file"
  perl -pi -e 's/Terminal\.Window\.Manager(?!\w)/Terminal.Window.WindowManager/g' "$file"
}

# Process all Elixir files
echo "Processing all Elixir files..."
count=0
for file in $(find lib test -type f \( -name "*.ex" -o -name "*.exs" \)); do
  update_file "$file"
  count=$((count + 1))
  if [ $((count % 50)) -eq 0 ]; then
    echo "  Processed $count files..."
  fi
done

echo "Completed processing $count files"
echo "Phase 2 complete: All terminal manager references updated!"