name: "windsurf-roocode-{account_name}"
base_image: "ubuntu-24.04-desktop"
memory: 8192
vcpus: 4
gui: true
spice: true

# ─── Instalacja narzędzi ──────────────────────────────
packages:
  - curl
  - wget
  - git
  - fonts-noto
  - fonts-noto-cjk

apps:
  - windsurf

# ─── Współdzielone projekty z hosta ──────────────────
paths:
  shared:
    - host: "~/github"
      guest: "/home/dev/github"
      mode: "rw"
    - host: "~/.ssh"
      guest: "/home/dev/.ssh"
      mode: "ro"

  # Klonuj konfigurację Windsurf z hosta (oprócz settings.json)
  clone:
    - "~/.config/Windsurf/User/snippets"
    - "~/.config/Windsurf/User/keybindings.json"

# ─── Cloud-init: post-install skrypty ────────────────
cloud_init:
  write_files:
    # Konfiguracja Windsurf + Roo Code (wstrzyknięta przez proxym)
    # Zmienne ${VM_API_KEY}, ${PROXY_HOST}, ${PROXY_PORT} są renderowane przez proxym
    - path: /home/dev/.config/Windsurf/User/settings.json
      permissions: '0600'
      owner: dev:dev
      content: |
        {
          "roo-cline.apiProvider": "openai",
          "roo-cline.openAiBaseUrl": "http://${PROXY_HOST}:${PROXY_PORT}",
          "roo-cline.openAiApiKey": "${VM_API_KEY}",
          "roo-cline.modeApiConfigs": {
            "code": {
              "apiProvider": "openai",
              "openAiBaseUrl": "http://${PROXY_HOST}:${PROXY_PORT}",
              "openAiApiKey": "${VM_API_KEY}",
              "openAiModelId": "balanced"
            },
            "architect": {
              "apiProvider": "openai",
              "openAiBaseUrl": "http://${PROXY_HOST}:${PROXY_PORT}",
              "openAiApiKey": "${VM_API_KEY}",
              "openAiModelId": "free"
            },
            "debug": {
              "apiProvider": "openai",
              "openAiBaseUrl": "http://${PROXY_HOST}:${PROXY_PORT}",
              "openAiApiKey": "${VM_API_KEY}",
              "openAiModelId": "cheap"
            },
            "ask": {
              "apiProvider": "openai",
              "openAiBaseUrl": "http://${PROXY_HOST}:${PROXY_PORT}",
              "openAiApiKey": "${VM_API_KEY}",
              "openAiModelId": "free"
            },
            "orchestrator": {
              "apiProvider": "openai",
              "openAiBaseUrl": "http://${PROXY_HOST}:${PROXY_PORT}",
              "openAiApiKey": "${VM_API_KEY}",
              "openAiModelId": "balanced"
            }
          },
          "roo-cline.customModes": [
            {
              "slug": "opus",
              "name": "Opus — Złożone zadania",
              "roleDefinition": "Expert architect for complex multi-file refactorings and challenging coding tasks.",
              "groups": ["read", "edit", "command"],
              "apiConfig": {
                "apiProvider": "openai",
                "openAiBaseUrl": "http://${PROXY_HOST}:${PROXY_PORT}",
                "openAiApiKey": "${VM_API_KEY}",
                "openAiModelId": "premium"
              }
            }
          ],
          "roo-cline.autoApproveMaxRequests": 10,
          "roo-cline.maxTokens": 8192,
          "windsurf.apiBase": "http://${PROXY_HOST}:${PROXY_PORT}/v1",
          "windsurf.byok.enabled": true
        }

    # Skrypt instalacji rozszerzenia Roo Code
    - path: /home/dev/setup-extensions.sh
      permissions: '0755'
      owner: dev:dev
      content: |
        #!/bin/bash
        set -e
        echo "Waiting for Windsurf to be available..."
        sleep 15
        
        # Sprawdź czy windsurf jest dostępny
        if ! command -v windsurf &> /dev/null; then
          echo "Windsurf not found, waiting longer..."
          sleep 30
        fi
        
        # Instalacja Roo Code jako rozszerzenie Windsurf (fork VS Code)
        echo "Installing Roo Code extension..."
        sudo -u dev windsurf --install-extension RooVetterinary.roo-cline --force || true
        echo "✅ Roo Code installation attempted"
        
        # Ustaw prawidłowe uprawnienia
        chown -R dev:dev /home/dev/.config/Windsurf || true

    # Konfiguracja Git (template zostanie wypełniony przez proxym)
    - path: /home/dev/.gitconfig
      permissions: '0644'
      owner: dev:dev
      content: |
        [user]
          name = ${GIT_USER_NAME}
          email = ${GIT_USER_EMAIL}
        [core]
          editor = windsurf --wait
        [init]
          defaultBranch = main

    # Skrypt weryfikacji połączenia z proxym
    - path: /home/dev/verify-proxy.sh
      permissions: '0755'
      owner: dev:dev
      content: |
        #!/bin/bash
        echo "Testing connection to proxym at ${PROXY_HOST}:${PROXY_PORT}..."
        curl -s http://${PROXY_HOST}:${PROXY_PORT}/health || echo "Health check failed"
        echo ""
        echo "Available models:"
        curl -s http://${PROXY_HOST}:${PROXY_PORT}/v1/models || echo "Models check failed"

  runcmd:
    # Zainstaluj Windsurf przez apt
    - curl -fsSL https://windsurf.codeium.com/windsurf.gpg | gpg --dearmor -o /usr/share/keyrings/windsurf.gpg
    - echo "deb [signed-by=/usr/share/keyrings/windsurf.gpg] https://windsurf.codeium.com/wsp/ stable main" > /etc/apt/sources.list.d/windsurf.list
    - apt-get update && apt-get install -y windsurf

    # Zainstaluj Roo Code po uruchomieniu Windsurf (w tle)
    - |
      sudo -u dev bash /home/dev/setup-extensions.sh &

    # Ustaw prawidłowe uprawnienia dla katalogu konfiguracyjnego
    - chown -R dev:dev /home/dev/.config || true

    # Usuń tymczasowy skrypt po instalacji (opcjonalnie)
    - rm -f /home/dev/setup-extensions.sh || true
