# Installation procedure for mcp-adp-bigmind (Windows)
# No external API token required — all data is stored locally.

param(
    [string]$SelectedIDEs = ""
)

$ErrorActionPreference = "Stop"
$BASEDIR = $PSScriptRoot
$MCP_NAME = "ADP BigMind"

function Test-PythonAndUV {
    $REQUIRED_VERSION = [Version]"3.12"
    try {
        $pythonVersion = python --version 2>$null
        if ($LASTEXITCODE -ne 0) { throw "Python is not installed or not in PATH" }
        if ($pythonVersion -match "Python (\d+\.\d+)\.") {
            $currentVersion = [Version]$matches[1]
            if ($currentVersion -lt $REQUIRED_VERSION) {
                throw "Python $currentVersion detected, but Python $REQUIRED_VERSION or higher is required"
            }
        }
    } catch {
        Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red; exit 1
    }
    try {
        uv --version 2>$null | Out-Null
        if ($LASTEXITCODE -ne 0) { throw "uv not found" }
    } catch {
        Write-Host "Installing uv package manager..." -ForegroundColor Yellow
        pip install uv
    }
}

try {
    Test-PythonAndUV

    # Install dependencies
    $venvPath = Join-Path $BASEDIR ".venv"
    if (-not (Test-Path $venvPath)) {
        Write-Host "Installing dependencies for $MCP_NAME..." -ForegroundColor Yellow
        Push-Location $BASEDIR
        uv sync
        Pop-Location
    }

    # Create BigMind DB directory
    $BigMindDir = Join-Path $env:USERPROFILE ".mcp\bigmind"
    New-Item -ItemType Directory -Force -Path $BigMindDir | Out-Null
    Write-Host "INFO: BigMind DB directory: $BigMindDir\memory.db" -ForegroundColor Cyan

    # Write Copilot instructions
    $DefaultWorkspace = Split-Path $BASEDIR -Parent
    Write-Host ""
    Write-Host "BigMind - GitHub Copilot Instructions Setup" -ForegroundColor Cyan
    Write-Host "Enter your workspace root path (press Enter for: $DefaultWorkspace):"
    $WorkspacePath = Read-Host
    if ([string]::IsNullOrWhiteSpace($WorkspacePath)) { $WorkspacePath = $DefaultWorkspace }

    $CopilotDir  = Join-Path $WorkspacePath ".github"
    $CopilotFile = Join-Path $CopilotDir "copilot-instructions.md"
    New-Item -ItemType Directory -Force -Path $CopilotDir | Out-Null

    $BigMindBlock = @"

## BigMind Persistent Memory

You have access to a BigMind persistent memory MCP server.

Rules (mandatory):
- ALWAYS call memory_start_session() as the very first action of every conversation.
- ALWAYS call memory_end_session() as the very last action before closing a conversation.
- Call memory_flag_important() whenever a significant decision, code change, or new
  preference is mentioned -- do not wait to be asked.
- If asked "do you remember...", call memory_search_chunks() or
  memory_get_session_detail() before answering from your own knowledge.
"@

    if (Test-Path $CopilotFile) {
        $existing = Get-Content $CopilotFile -Raw
        if ($existing -match "BigMind Persistent Memory") {
            Write-Host "INFO: BigMind instructions already present — skipping." -ForegroundColor Yellow
        } else {
            Add-Content -Path $CopilotFile -Value $BigMindBlock
            Write-Host "INFO: BigMind instructions appended to $CopilotFile" -ForegroundColor Green
        }
    } else {
        "# Copilot Instructions`n$BigMindBlock" | Set-Content -Path $CopilotFile
        Write-Host "INFO: Created $CopilotFile with BigMind instructions" -ForegroundColor Green
    }

    # Output MCP config
    Write-Output "MCP_NAME=$MCP_NAME"
    Write-Output "MCP_SNIPPET="
    @{
        command = "uv"
        args    = @("--directory", $BASEDIR, "run", "src/server.py")
        env     = @{ BIGMIND_USER = $env:USERNAME }
    } | ConvertTo-Json -Depth 10

} catch {
    Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

