Functions/Ses/Sesio-Arc.ps1

function Sesio-Arc {
    param (
        [Parameter(Position=0, Mandatory=$true)]
        [string]$Command,
        
        [Parameter(ValueFromRemainingArguments=$true)]
        $RemainingArgs
    )

    switch ($Command.ToLower()) {
        "create" {
            $params = @{}
            for ($i = 0; $i -lt $RemainingArgs.Count; $i += 2) {
                if ($i + 1 -lt $RemainingArgs.Count) {
                    $params[$RemainingArgs[$i].TrimStart('-')] = $RemainingArgs[$i + 1]
                }
            }
            Arc-Create @params
        }
        "update"{
            $params = @{}
            for ($i = 0; $i -lt $RemainingArgs.Count; $i++) {
                if ($RemainingArgs[$i] -eq '-o' -or $RemainingArgs[$i] -eq '-open') {
                    $params['OpenFile'] = 'notepad'
                    continue
                }
                if ($i + 1 -lt $RemainingArgs.Count) {
                    $params[$RemainingArgs[$i].TrimStart('-')] = $RemainingArgs[$i + 1]
                    $i++
                }
            }
            Arc-Update @params
        }
        "list" { Arc-List @RemainingArgs }
        "help" { Arc-Help }
        default { Write-Host "Unknown command: $Command" }
    }
}   

function Arc-Update {
    param(
        [Parameter(Mandatory=$false)]
        [Alias("file")]
        [string]$FileName,
        
        [Parameter(Mandatory=$false)]
        [Alias("env")]
        [string]$Environment,

        [Parameter(Mandatory=$false)]
        [Alias("open")]
        [string]$OpenFile
    )
    
  if (-not $FileName) {   
        $FileName = Read-Host "Enter the file name"
    }

    if (-not $Environment) {
        $Environment = Read-Host "Enter the environment name"
    }
    if (-not $OpenFile) {
        $OpenFile = Read-Host "Enter the open command (code or notepad). press Enter to use notepad."
    }

    if (-not $FileName -or -not $Environment) {
        Write-Host "File name and environment are required"
        return
    }   
    
    $sesioFolder = Join-Path $env:USERPROFILE ".sesio"
    $arcFileName = "$Environment.$FileName.aarc"
    $arcFilePath = Join-Path $sesioFolder $arcFileName

    if (-not (Test-Path $arcFilePath)) {
        Write-Host "File not found: $arcFilePath"
        return
    }
    switch ($OpenFile) {
        'code' { code $arcFilePath }
        default { notepad $arcFilePath }
    }
    return
}

function Arc-Create {
    param(
        [Parameter(Mandatory=$false)]
        [Alias("file")]
        [string]$FileName,
        
        [Parameter(Mandatory=$false)]
        [Alias("env")]
        [string]$Environment
    )

    if (-not $FileName) {   
        $FileName = Read-Host "Enter the file name"
    }

    if (-not $Environment) {
        $Environment = Read-Host "Enter the environment name"
    }

    if (-not $FileName -or -not $Environment) {
        Write-Host "File name and environment are required"
        return
    }   

    # Create .sesio folder if it doesn't exist
    $sesioFolder = Join-Path $env:USERPROFILE ".sesio"
    if (-not (Test-Path $sesioFolder)) {
        New-Item -ItemType Directory -Path $sesioFolder -Force | Out-Null
        Write-Host "Created .sesio folder: $sesioFolder"
    } else {
        Write-Host ".sesio folder already exists: $sesioFolder"
    }

    # Create arc file
    $arcFileName = "$Environment.$FileName.aarc"
    $arcFilePath = Join-Path $sesioFolder $arcFileName
    New-Item -ItemType File -Path $arcFilePath -Force | Out-Null

    if (Test-Path $arcFilePath) {
        Write-Host "Arc file created successfully: $arcFilePath"
    } else {
        Write-Host "Failed to create arc file: $arcFilePath"
    }
}

function Arc-List {
    param(
        [Alias("e")][string]$Environment
    )
    # Get the path to the .sesio folder
    $sesioFolder = Join-Path $env:USERPROFILE ".sesio"
   
    # Check if the .sesio folder exists
    if (-not (Test-Path $sesioFolder)) {
        Write-Host "No .sesio folder found at $sesioFolder. No arc files to list."
        return
    }

    if(-not $Environment) {
        $arcFiles = Get-ChildItem -Path $sesioFolder -Filter "*.aarc"
    }
    else {
        $arcFiles = Get-ChildItem -Path $sesioFolder -Filter "$Environment.*.aarc"
    }


    if ($arcFiles.Count -eq 0) {
        Write-Host "No .aarc files found in the .sesio folder."
    } else {
        Write-Host ""
        Write-Host "$([char]0x1b)[34mFound $($arcFiles.Count) .aarc files.$([char]0x1b)[0m"
        $groupedFiles = $arcFiles | Group-Object { ($_.BaseName -split '\.')[0] }
        
        foreach ($group in $groupedFiles) {
            Write-Host "`n$([char]0x1b)[32mEnvironment:$([char]0x1b)[0m $($group.Name)"
            Write-Host "$([char]0x1b)[32m--------------------------------$([char]0x1b)[0m"
            foreach ($file in $group.Group) {
                $parts = $file.BaseName -split '\.'
                if ($parts.Count -ge 2) {
                    $fileName = $parts[1]
                    Write-Host " - $([char]0x1b)[33m$fileName$([char]0x1b)[0m"
                } else {
                    Write-Host " - $([char]0x1b)[33m$($file.Name)$([char]0x1b)[0m (Unexpected format)"
                }
            }
        }
        Write-Host "$([char]0x1b)[32m--------------------------------$([char]0x1b)[0m"
    }
}

function Arc-Help {
    Write-Host "$([char]0x1b)[32mSesio Arc Help$([char]0x1b)[0m"
    Write-Host ""
    Write-Host "$([char]0x1b)[32mUsage:$([char]0x1b)[0m Sesio arc <command> [options]"
    Write-Host "$([char]0x1b)[32mCommands:$([char]0x1b)[0m"
    Write-Host "$([char]0x1b)[32m--------------------------------$([char]0x1b)[0m"
    Write-Host "$([char]0x1b)[31mcreate$([char]0x1b)[0m - Create a new arc file"
    Write-Host "$([char]0x1b)[31mlist$([char]0x1b)[0m - List all arc files"
    Write-Host "$([char]0x1b)[31mhelp$([char]0x1b)[0m - Show help for a command"
    Write-Host "$([char]0x1b)[32m--------------------------------$([char]0x1b)[0m"
}

Export-ModuleMember -Function Sesio-Arc