# OpsTrail PowerShell Integration Installer

$OpsTrailIntegration = @'

# OpsTrail - Terminal Activity Tracker Integration
$global:OpsTrailSessionStarted = $false

# Start session on profile load
if (-not $global:OpsTrailSessionStarted) {
    & trail log --session-start 2>$null
    $global:OpsTrailSessionStarted = $true
}

# Capture command history after execution
function global:OpsTrail-LogCommand {
    # Get the last command from history
    $lastCmd = Get-History -Count 1 -ErrorAction SilentlyContinue

    if ($lastCmd) {
        $cmd = $lastCmd.CommandLine

        # Skip trail commands to avoid recursion
        if ($cmd -like "trail *" -or $cmd -like "opstrail *" -or $cmd -like "*OpsTrail*") {
            return
        }

        $cwd = $PWD.Path

        # Log the command
        & trail log --cmd "$cmd" --cwd "$cwd" 2>$null
    }
}

# Wrap the prompt to log after each command
$global:OpsTrail_OriginalPrompt = $function:prompt

function global:prompt {
    OpsTrail-LogCommand
    & $global:OpsTrail_OriginalPrompt
}

# Register session end on exit
# Store trail path to ensure it's accessible during exit
$global:OpsTrail_TrailPath = (Get-Command trail -ErrorAction SilentlyContinue).Source
if (-not $global:OpsTrail_TrailPath) {
    $global:OpsTrail_TrailPath = (Get-Command trail.exe -ErrorAction SilentlyContinue).Source
}

# Create the action as a script block that captures the path
$exitAction = [scriptblock]::Create(@"
    try {
        `$trailPath = '$($global:OpsTrail_TrailPath)'
        if (`$trailPath -and (Test-Path `$trailPath)) {
            & `$trailPath log --session-end
        } else {
            & trail log --session-end 2>`$null
        }
    } catch {
        # Silently fail - we're exiting anyway
    }
"@)

Register-EngineEvent PowerShell.Exiting -Action $exitAction | Out-Null

# Helper function: Jump back in time
function global:trail-back {
    param([string]$when = "30m")
    $path = & trail back $when 2>$null
    if ($LASTEXITCODE -eq 0 -and $path -and (Test-Path $path)) {
        Set-Location $path
        Write-Host "Jumped back to: $path" -ForegroundColor Green
    } else {
        Write-Host "No activity found for that time" -ForegroundColor Red
    }
}

# Helper function: Resume last session
function global:trail-resume {
    $output = & trail resume 2>&1

    # Extract path from resume output
    $pathLine = $output | Select-String -Pattern "Path:\s*(.+)"

    if ($pathLine) {
        Write-Host $output
        $path = $pathLine.Matches.Groups[1].Value.Trim()

        if (Test-Path $path) {
            Write-Host ""
            $response = Read-Host "Jump to this location? (y/n)"
            if ($response -eq 'y' -or $response -eq 'Y') {
                Set-Location $path
                Write-Host "Resumed at: $path" -ForegroundColor Green
            }
        }
    } else {
        Write-Host $output
    }
}

# Override trail command to respect auto-cd config
function global:trail {
    param(
        [Parameter(Position=0)]
        [string]$subcommand,

        [Parameter(Position=1, ValueFromRemainingArguments=$true)]
        [string[]]$args
    )

    # For 'back' command, check config for auto-cd
    if ($subcommand -eq "back" -and $args.Count -gt 0) {
        $when = $args[0]

        # Check if auto-cd is enabled for 'back'
        $configPath = Join-Path $env:USERPROFILE ".opstrail\config.json"
        $autoCdEnabled = $true  # Default to true

        if (Test-Path $configPath) {
            try {
                $config = Get-Content $configPath | ConvertFrom-Json
                if ($null -ne $config.auto_cd -and $null -ne $config.auto_cd.back) {
                    $autoCdEnabled = $config.auto_cd.back
                }
            } catch {
                # If config parse fails, use default (true)
            }
        }

        if ($autoCdEnabled) {
            # Auto-cd enabled: change directory
            $path = & trail.exe back $when 2>$null

            if ($LASTEXITCODE -eq 0 -and $path -and (Test-Path $path)) {
                Set-Location $path
                Write-Host "Jumped back $when to: $path" -ForegroundColor Green
            } else {
                Write-Host "No activity found for '$when'" -ForegroundColor Red
            }
        } else {
            # Auto-cd disabled: just show the path
            & trail.exe back $when
        }
        return
    }

    # For 'resume' command, check config for auto-cd
    if ($subcommand -eq "resume") {
        # Check if auto-cd is enabled for 'resume'
        $configPath = Join-Path $env:USERPROFILE ".opstrail\config.json"
        $autoCdEnabled = $true  # Default to true

        if (Test-Path $configPath) {
            try {
                $config = Get-Content $configPath | ConvertFrom-Json
                if ($null -ne $config.auto_cd -and $null -ne $config.auto_cd.resume) {
                    $autoCdEnabled = $config.auto_cd.resume
                }
            } catch {
                # If config parse fails, use default (true)
            }
        }

        if ($autoCdEnabled) {
            # Auto-cd enabled: show output and prompt to jump
            $output = & trail.exe resume 2>&1
            $pathLine = $output | Select-String -Pattern "^[A-Z]:"

            if ($pathLine) {
                # Extract the last line which should be the path
                $path = $pathLine.Matches.Value | Select-Object -Last 1

                if (Test-Path $path) {
                    # Show the resume info first
                    $output | Where-Object { $_ -notmatch "^[A-Z]:" } | ForEach-Object { Write-Host $_ }

                    Write-Host ""
                    $response = Read-Host "Jump to this location? (y/n)"
                    if ($response -eq 'y' -or $response -eq 'Y') {
                        Set-Location $path
                        Write-Host "Resumed at: $path" -ForegroundColor Green
                    }
                } else {
                    Write-Host $output
                }
            } else {
                Write-Host $output
            }
        } else {
            # Auto-cd disabled: just show the output
            & trail.exe resume
        }
        return
    }

    # For all other commands, pass through to the real trail executable
    & trail.exe $subcommand @args
}

Write-Host "OpsTrail tracking enabled" -ForegroundColor Cyan
'@

# Check if profile exists
if (-not (Test-Path $PROFILE)) {
    New-Item -ItemType File -Path $PROFILE -Force | Out-Null
    Write-Host "Created PowerShell profile at: $PROFILE" -ForegroundColor Green
}

# Check if already installed
$profileContent = Get-Content $PROFILE -Raw -ErrorAction SilentlyContinue

if ($profileContent -like "*OpsTrail*") {
    Write-Host "OpsTrail integration is already installed!" -ForegroundColor Yellow
    $response = Read-Host "Do you want to reinstall? (y/N)"
    if ($response -ne 'y' -and $response -ne 'Y') {
        exit
    }

    # Remove old integration
    $profileContent = $profileContent -replace '(?s)# OpsTrail.*?Write-Host.*OpsTrail tracking enabled.*?\n', ''
    Set-Content $PROFILE $profileContent.Trim()
}

# Add integration
Add-Content $PROFILE "`n$OpsTrailIntegration"

Write-Host ""
Write-Host "OpsTrail PowerShell integration installed!" -ForegroundColor Green
Write-Host ""
Write-Host "Reload your profile to activate:" -ForegroundColor Cyan
Write-Host "  . `$PROFILE" -ForegroundColor Yellow
Write-Host ""
Write-Host "Useful commands:" -ForegroundColor Cyan
Write-Host "  trail today          - Today summary" -ForegroundColor White
Write-Host "  trail timeline       - View activity timeline" -ForegroundColor White
Write-Host "  trail stats          - Activity statistics" -ForegroundColor White
Write-Host "  trail search <term>  - Search your history" -ForegroundColor White
Write-Host "  trail back 1h        - Where was I an hour ago" -ForegroundColor White
Write-Host "  trail-back 30m       - Jump back 30 minutes" -ForegroundColor White
Write-Host "  trail-resume         - Resume last session" -ForegroundColor White
Write-Host "  trail note <text>    - Add a note" -ForegroundColor White
Write-Host ""
