tt.ps1

#Requires -Version 5.1
<#
.SYNOPSIS
    tt - Quick CLI wrapper for TerminalTracker.
.DESCRIPTION
    Shorthand commands for common TerminalTracker operations.
.EXAMPLE
    tt # Show dashboard
    tt list # List active sessions
    tt list -all # List all including hidden
    tt note abc "text" # Set note on session abc
    tt suspend # Save & close current window
    tt suspend abc # Save & close session abc
    tt resume # List suspended / resume all
    tt resume abc # Resume specific session
    tt resume -all # Resume all suspended
    tt hide abc # Hide window
    tt show abc # Show window
    tt archive # View archive
    tt archive -clear # Clear archive
    tt open C:\Dev\proj # Open new terminal at path
    tt export C:\Sync # Export state
    tt import C:\file.json # Import state
    tt config # Show config
    tt monitor # Start monitor
    tt install # Run installer
    tt uninstall # Uninstall
#>

[CmdletBinding()]
param(
    [Parameter(Position = 0)]
    [string]$Command,

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

# Ensure module is loaded
$modulePath = Join-Path $PSScriptRoot 'TerminalTracker.psm1'
if (-not (Get-Module TerminalTracker)) {
    Import-Module $modulePath -Force -DisableNameChecking
}

# Parse -flags from Args
$flags = @{}
$positional = @()
foreach ($a in $Args) {
    if ($a -match '^-(\w+)$') {
        $flags[$Matches[1].ToLower()] = $true
    } else {
        $positional += $a
    }
}

switch ($Command) {
    { $_ -in '', 'dashboard', 'dash', 'status' } {
        Show-TTDashboard
    }

    'scan' {
        Scan-TTTerminals | Out-Null
        Show-TTDashboard
    }

    { $_ -in 'list', 'ls', 'sessions' } {
        $params = @{}
        if ($flags['all'])    { $params['IncludeHidden'] = $true }
        if ($flags['active']) { $params['Active'] = $true }
        if ($positional.Count -gt 0) { $params['Id'] = $positional[0] }

        $sessions = Get-TTSession @params
        if (-not $sessions) {
            Write-Host "No sessions found." -ForegroundColor Gray
            return
        }
        foreach ($s in $sessions) {
            $status = if ($s.IsAlive) { 'LIVE' } else { 'DEAD' }
            $color  = if ($s.IsAlive) { 'Green' } else { 'Red' }
            $hidden = if ($s.Hidden) { ' [H]' } else { '' }
            Write-Host "$($s.Id)" -ForegroundColor White -NoNewline
            Write-Host " [$status]$hidden" -ForegroundColor $color -NoNewline
            Write-Host " $($s.WorkingDirectory)" -ForegroundColor Gray -NoNewline
            if ($s.Notes) { Write-Host " | $($s.Notes)" -ForegroundColor Yellow -NoNewline }
            Write-Host ""
        }
    }

    'note' {
        if ($positional.Count -lt 2) {
            Write-Host "Usage: tt note <id> <text> [-append]" -ForegroundColor Yellow
            return
        }
        $params = @{ Id = $positional[0]; Note = ($positional[1..($positional.Count-1)] -join ' ') }
        if ($flags['append']) { $params['Append'] = $true }
        Set-TTNote @params
    }

    'tag' {
        if ($positional.Count -lt 2) {
            Write-Host "Usage: tt tag <id> <tag> [-remove]" -ForegroundColor Yellow
            return
        }
        $params = @{ Id = $positional[0] }
        if ($flags['remove']) { $params['Remove'] = @($positional[1]) }
        else { $params['Add'] = @($positional[1]) }
        Set-TTTag @params
    }

    { $_ -in 'suspend', 'save' } {
        $params = @{}
        if ($positional.Count -gt 0) { $params['Id'] = $positional[0] }
        if ($positional.Count -gt 1) { $params['Note'] = ($positional[1..($positional.Count-1)] -join ' ') }
        Suspend-TTSession @params
    }

    'resume' {
        $params = @{}
        if ($flags['all']) { $params['All'] = $true }
        if ($positional.Count -gt 0) { $params['Id'] = $positional[0] }
        Resume-TTSession @params
    }

    'suspended' {
        $suspended = Get-TTSuspended
        if (-not $suspended) { Write-Host "No suspended sessions." -ForegroundColor Gray; return }
        foreach ($s in $suspended) {
            Write-Host "$($s.Id)" -ForegroundColor White -NoNewline
            Write-Host " $($s.WorkingDirectory)" -ForegroundColor Gray -NoNewline
            if ($s.Notes) { Write-Host " | $($s.Notes)" -ForegroundColor Yellow -NoNewline }
            Write-Host ""
        }
    }

    'hide' {
        if (-not $positional) { Write-Host "Usage: tt hide <id>" -ForegroundColor Yellow; return }
        Hide-TTWindow -Id $positional[0]
    }

    'show' {
        if (-not $positional) { Write-Host "Usage: tt show <id>" -ForegroundColor Yellow; return }
        Show-TTWindow -Id $positional[0]
    }

    'remove' {
        if (-not $positional) { Write-Host "Usage: tt remove <id>" -ForegroundColor Yellow; return }
        Remove-TTSession -Id $positional[0]
    }

    'archive' {
        if ($flags['clear']) {
            $days = if ($positional.Count -gt 0) { [int]$positional[0] } else { $null }
            if ($days) { Clear-TTArchive -OlderThanDays $days }
            else { Clear-TTArchive }
        }
        else {
            $params = @{}
            if ($positional.Count -gt 0) { $params['Last'] = [int]$positional[0] }
            $archive = Get-TTArchive @params
            if (-not $archive) { Write-Host "Archive is empty." -ForegroundColor Gray; return }
            foreach ($a in $archive) {
                Write-Host "$($a.Id)" -ForegroundColor DarkGray -NoNewline
                Write-Host " $($a.CloseReason)" -ForegroundColor DarkYellow -NoNewline
                Write-Host " $($a.WorkingDirectory)" -ForegroundColor Gray -NoNewline
                Write-Host " ($($a.ClosedTime))" -ForegroundColor DarkGray
            }
        }
    }

    'open' {
        if (-not $positional) { Write-Host "Usage: tt open <directory> [-shell pwsh|cmd|bash]" -ForegroundColor Yellow; return }
        $params = @{ Directory = $positional[0] }
        if ($positional.Count -gt 1) { $params['Shell'] = $positional[1] }
        Open-TerminalAt @params
    }

    'export' {
        if (-not $positional) { Write-Host "Usage: tt export <path>" -ForegroundColor Yellow; return }
        Export-TTState -Path $positional[0]
    }

    'import' {
        if (-not $positional) { Write-Host "Usage: tt import <file>" -ForegroundColor Yellow; return }
        $params = @{ Path = $positional[0] }
        if ($flags['suspended']) { $params['SuspendedOnly'] = $true }
        if ($flags['merge'])     { $params['MergeArchive'] = $true }
        Import-TTState @params
    }

    'config' {
        if ($positional.Count -eq 0) {
            Get-TTConfig | Format-List
        }
        else {
            $params = @{}
            for ($i = 0; $i -lt $positional.Count; $i += 2) {
                $key = $positional[$i]
                $val = if ($i + 1 -lt $positional.Count) { $positional[$i + 1] } else { $true }
                # Coerce types
                if ($val -eq 'true')  { $val = $true }
                if ($val -eq 'false') { $val = $false }
                if ($val -match '^\d+$') { $val = [int]$val }
                $params[$key] = $val
            }
            Set-TTConfig @params
        }
    }

    'monitor' {
        if ($flags['stop']) { Stop-TTMonitor }
        else { Start-TTMonitor -AsJob }
    }

    'install' {
        & (Join-Path $PSScriptRoot 'Install-TerminalTracker.ps1')
    }

    'uninstall' {
        & (Join-Path $PSScriptRoot 'Install-TerminalTracker.ps1') -Uninstall
    }

    'help' {
        Write-Host ""
        Write-Host " tt - TerminalTracker CLI" -ForegroundColor Cyan
        Write-Host ""
        Write-Host " Commands:" -ForegroundColor White
        Write-Host " (none), dashboard Show session dashboard" -ForegroundColor Gray
        Write-Host " scan Discover terminals & show dashboard" -ForegroundColor Gray
        Write-Host " list, ls List active sessions" -ForegroundColor Gray
        Write-Host " note <id> <text> Set resume note" -ForegroundColor Gray
        Write-Host " tag <id> <tag> Add tag (-remove to delete)" -ForegroundColor Gray
        Write-Host " suspend [id] Save & close (current if no id)" -ForegroundColor Gray
        Write-Host " resume [id] [-all] Restore suspended sessions" -ForegroundColor Gray
        Write-Host " suspended List suspended sessions" -ForegroundColor Gray
        Write-Host " hide <id> Hide window from taskbar" -ForegroundColor Gray
        Write-Host " show <id> Show hidden window" -ForegroundColor Gray
        Write-Host " remove <id> Remove session (archives first)" -ForegroundColor Gray
        Write-Host " archive [n] View last n archive entries" -ForegroundColor Gray
        Write-Host " archive -clear [d] Clear archive (optionally >d days)" -ForegroundColor Gray
        Write-Host " open <dir> [shell] Open terminal at directory" -ForegroundColor Gray
        Write-Host " export <path> Export state for sync" -ForegroundColor Gray
        Write-Host " import <file> Import state from sync" -ForegroundColor Gray
        Write-Host " config [k v ...] View or set config" -ForegroundColor Gray
        Write-Host " monitor [-stop] Start/stop background monitor" -ForegroundColor Gray
        Write-Host " install Run setup wizard" -ForegroundColor Gray
        Write-Host " uninstall Remove hooks and auto-start" -ForegroundColor Gray
        Write-Host ""
    }

    default {
        Write-Host "Unknown command: $Command. Use 'tt help' for usage." -ForegroundColor Red
    }
}