#!/bin/bash
# Companion AI - Windows Installation Script

Write-Host "Companion AI Installation Script for Windows"
Write-Host "============================================="
Write-Host ""

# Check if Python is installed
$python = Get-Command python -ErrorAction SilentlyContinue
if (-not $python) {
    Write-Error "Python 3 is required but not found. Please install Python 3.11 or later."
    exit 1
}

$pythonVersion = python --version 2>&1
Write-Host "Found: $pythonVersion"

# Create directories
$installDir = "$env:LOCALAPPDATA\Companion"
$dataDir = "$env:LOCALAPPDATA\Companion\Data"

Write-Host "Creating directories..."
New-Item -ItemType Directory -Force -Path $installDir | Out-Null
New-Item -ItemType Directory -Force -Path $dataDir\vectors | Out-Null
New-Item -ItemType Directory -Force -Path $dataDir\memory | Out-Null
New-Item -ItemType Directory -Force -Path $dataDir\models | Out-Null

# Copy application files
Write-Host "Installing Companion AI..."
$sourceDir = $PSScriptRoot
if (Test-Path "$sourceDir\src") {
    Copy-Item -Recurse -Force "$sourceDir\*" $installDir
} else {
    Write-Error "Cannot find source files. Please run from the Companion AI directory."
    exit 1
}

# Create virtual environment
Write-Host "Creating Python virtual environment..."
Set-Location $installDir
python -m venv venv

# Activate and install
Write-Host "Installing dependencies..."
& .\venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install -e ".[dev]"

# Copy config
if (-not (Test-Path "$dataDir\config.json")) {
    Copy-Item "$installDir\config.json" "$dataDir\config.json"
    # Update paths
    $config = Get-Content "$dataDir\config.json" -Raw
    $config = $config -replace '~/.companion', $dataDir.Replace('\', '/')
    Set-Content "$dataDir\config.json" $config
}

# Create startup scripts
Write-Host "Creating startup scripts..."

# API server startup script
$apiScript = @"
@echo off
set PYTHONPATH=$($installDir)
set COMPANION_CONFIG=$($dataDir)\config.json
set COMPANION_DATA_DIR=$($dataDir)
cd "$($installDir)"
.\venv\Scripts\python -m uvicorn companion.api:app --host 0.0.0.0 --port 7373
"@
Set-Content "$installDir\start-api.bat" $apiScript

# Indexer watcher startup script
$indexerScript = @"
@echo off
set PYTHONPATH=$($installDir)
set COMPANION_CONFIG=$($dataDir)\config.json
set COMPANION_DATA_DIR=$($dataDir)
cd "$($installDir)"
.\venv\Scripts\python -m companion.indexer_daemon.watcher
"@
Set-Content "$installDir\start-indexer.bat" $indexerScript

# CLI shortcut
$cliScript = @"
@echo off
set PYTHONPATH=$($installDir)
set COMPANION_CONFIG=$($dataDir)\config.json
set COMPANION_DATA_DIR=$($dataDir)
cd "$($installDir)"
.\venv\Scripts\python -m companion.indexer_daemon.cli %*
"@
Set-Content "$installDir\companion.bat" $cliScript

Write-Host ""
Write-Host "Installation complete!"
Write-Host "====================="
Write-Host ""
Write-Host "To start the API server:"
Write-Host "  $installDir\start-api.bat"
Write-Host ""
Write-Host "To start the file watcher (auto-indexing):"
Write-Host "  $installDir\start-indexer.bat"
Write-Host ""
Write-Host "To run a manual index:"
Write-Host "  $installDir\companion.bat index"
Write-Host ""
Write-Host "Config location: $dataDir\config.json"
Write-Host "Data location: $dataDir"
Write-Host ""
Write-Host "Edit the config to set your vault path, then start the services."
