#!/usr/bin/env bash
# Desktop taskserv preparation script

set -euo pipefail

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

# Create desktop user home directory structure
prepare_user_directories() {
    local desktop_user="${DESKTOP_USER:-desktop}"
    local desktop_home="${DESKTOP_HOME:-/home/$desktop_user}"
    
    log "Preparing directories for user $desktop_user"
    
    # Create standard user directories
    mkdir -p "$desktop_home"/{Desktop,Documents,Downloads,Pictures,Videos,Music}
    mkdir -p "$desktop_home"/.config
    mkdir -p "$desktop_home"/.local/{bin,share}
    
    # Set proper ownership if user exists
    if id "$desktop_user" &>/dev/null; then
        chown -R "$desktop_user:$desktop_user" "$desktop_home"
    fi
}

# Download application assets
download_assets() {
    log "Downloading application assets..."
    
    # Create assets directory
    mkdir -p /tmp/desktop-assets
    
    # Download Zed editor GPG key for verification
    if command -v curl >/dev/null 2>&1; then
        curl -fsSL https://zed.dev/install.sh > /tmp/desktop-assets/zed-install.sh
        chmod +x /tmp/desktop-assets/zed-install.sh
    fi
}

# Prepare configuration templates
prepare_configs() {
    log "Preparing configuration templates..."
    
    # Create XFCE configuration template
    mkdir -p /tmp/desktop-configs/xfce4
    
    cat > /tmp/desktop-configs/xfce4/desktop.xml << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<channel name="xfce4-desktop" version="1.0">
  <property name="backdrop" type="empty">
    <property name="screen0" type="empty">
      <property name="monitor0" type="empty">
        <property name="workspace0" type="empty">
          <property name="color-style" type="int" value="0"/>
          <property name="image-style" type="int" value="5"/>
          <property name="last-image" type="string" value="/usr/share/pixmaps/xfce-blue.jpg"/>
        </property>
      </property>
    </property>
  </property>
</channel>
EOF

    # Create application menu template
    cat > /tmp/desktop-configs/applications.menu << 'EOF'
<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"
  "http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd">
<Menu>
    <Name>Applications</Name>
    <Directory>X-GNOME-Menu-Applications.directory</Directory>
    
    <Menu>
        <Name>Development</Name>
        <Directory>Development.directory</Directory>
        <Include>
            <Category>Development</Category>
        </Include>
    </Menu>
    
    <Menu>
        <Name>Graphics</Name>
        <Directory>Graphics.directory</Directory>
        <Include>
            <Category>Graphics</Category>
        </Include>
    </Menu>
    
    <Menu>
        <Name>Internet</Name>
        <Directory>Network.directory</Directory>
        <Include>
            <Category>Network</Category>
        </Include>
    </Menu>
    
    <Menu>
        <Name>Office</Name>
        <Directory>Office.directory</Directory>
        <Include>
            <Category>Office</Category>
        </Include>
    </Menu>
    
    <Menu>
        <Name>System</Name>
        <Directory>System-Tools.directory</Directory>
        <Include>
            <Category>System</Category>
        </Include>
    </Menu>
</Menu>
EOF
}

# Main preparation function
main() {
    log "Starting desktop taskserv preparation..."
    
    prepare_user_directories
    download_assets
    prepare_configs
    
    log "Desktop taskserv preparation completed!"
}

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