#!/usr/bin/env bash
# Zed Editor Setup Script for Desktop Environment

set -euo pipefail

log() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] ZED: $1"
}

error() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] ZED ERROR: $1" >&2
    exit 1
}

# Install Zed editor
install_zed() {
    local desktop_user="${DESKTOP_USER:-desktop}"
    
    log "Installing Zed editor for user $desktop_user"
    
    # Check architecture
    local arch=$(uname -m)
    case $arch in
        x86_64)
            log "Installing Zed for x86_64 architecture"
            ;;
        aarch64|arm64)
            log "Installing Zed for ARM64 architecture"
            ;;
        *)
            log "WARNING: Zed may not be available for $arch architecture"
            return 0
            ;;
    esac
    
    # Download and install Zed
    if command -v curl >/dev/null 2>&1; then
        # Install system-wide
        curl -f https://zed.dev/install.sh | sh
        
        # Also install for the desktop user
        sudo -u "$desktop_user" bash -c 'curl -f https://zed.dev/install.sh | sh'
    else
        error "curl not found - required for Zed installation"
    fi
}

# Configure Zed for desktop user
configure_zed() {
    local desktop_user="${DESKTOP_USER:-desktop}"
    local desktop_home="${DESKTOP_HOME:-/home/$desktop_user}"
    
    log "Configuring Zed editor for $desktop_user"
    
    # Create Zed config directory
    sudo -u "$desktop_user" mkdir -p "$desktop_home/.config/zed"
    
    # Create basic Zed configuration
    cat > "$desktop_home/.config/zed/settings.json" << 'EOF'
{
  "assistant": {
    "default_model": {
      "provider": "zed.dev",
      "model": "claude-3-5-sonnet-20241022"
    },
    "version": "2"
  },
  "vim_mode": false,
  "ui_font_size": 16,
  "buffer_font_size": 14,
  "theme": {
    "mode": "system",
    "light": "One Light",
    "dark": "One Dark"
  },
  "project_panel": {
    "dock": "left"
  },
  "outline_panel": {
    "dock": "right"
  },
  "collaboration_panel": {
    "dock": "left"
  },
  "chat_panel": {
    "dock": "right"
  },
  "notification_panel": {
    "dock": "right"
  },
  "terminal": {
    "dock": "bottom"
  },
  "git": {
    "git_gutter": "tracked_files",
    "inline_blame": {
      "enabled": true
    }
  },
  "lsp": {
    "rust-analyzer": {
      "binary": {
        "path_lookup": true
      }
    }
  },
  "languages": {
    "Python": {
      "format_on_save": "on",
      "formatter": "auto"
    },
    "JavaScript": {
      "format_on_save": "on"
    },
    "TypeScript": {
      "format_on_save": "on"
    },
    "Rust": {
      "format_on_save": "on"
    },
    "Go": {
      "format_on_save": "on"
    }
  },
  "auto_update": true,
  "telemetry": {
    "diagnostics": false,
    "metrics": false
  }
}
EOF
    
    # Set proper ownership
    chown -R "$desktop_user:$desktop_user" "$desktop_home/.config/zed"
    
    log "Zed configuration created"
}

# Create desktop shortcut for Zed
create_desktop_shortcut() {
    local desktop_user="${DESKTOP_USER:-desktop}"
    local desktop_home="${DESKTOP_HOME:-/home/$desktop_user}"
    
    log "Creating desktop shortcut for Zed"
    
    # Create desktop shortcut
    cat > "$desktop_home/Desktop/zed.desktop" << 'EOF'
[Desktop Entry]
Version=1.0
Type=Application
Name=Zed
Comment=A high-performance, multiplayer code editor
Exec=zed %F
Icon=zed
Terminal=false
MimeType=text/plain;text/x-chdr;text/x-csrc;text/x-c++hdr;text/x-c++src;text/x-java;text/x-dsrc;text/x-pascal;text/x-perl;text/x-python;application/x-php;application/x-httpd-php3;application/x-httpd-php4;application/x-httpd-php5;application/x-ruby;text/x-tcl;text/x-tex;application/x-sh;text/x-chdr;text/x-csrc;text/css;text/html;text/xml;text/javascript;application/javascript;application/json;text/x-markdown;text/x-rust;text/x-go;
StartupNotify=true
Categories=Development;TextEditor;
Keywords=editor;development;programming;
EOF
    
    chmod +x "$desktop_home/Desktop/zed.desktop"
    chown "$desktop_user:$desktop_user" "$desktop_home/Desktop/zed.desktop"
    
    log "Desktop shortcut created"
}

# Main function
main() {
    log "Starting Zed editor setup..."
    
    install_zed
    configure_zed
    create_desktop_shortcut
    
    log "Zed editor setup completed!"
}

# Run main function if script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi