Public/Adopt.ps1

# Adopting a company package update.
#
# The company installer writes its own settings into ~/.claude/settings.json and,
# in the plugin step, rewrites that file whether or not you declined the settings
# overwrite. So the update dance is always: back up your personal settings, run
# install.bat, move the company settings into the work profile, restore yours.
# `ccs adopt` is the second half of that, so the only manual step is install.bat.

function Backup-CcsPersonalSettings {
    <#
    .SYNOPSIS
        Snapshot the current ~/.claude/settings.json as the personal baseline.
    .DESCRIPTION
        Kept at a fixed path so `ccs adopt` can restore it after the company
        installer overwrites the live file. Refuses to overwrite the baseline
        with company settings, which would destroy the thing it protects.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param([switch]$Force)

    $live = Get-CcsSettingsPath -ProfileDir (Get-CcsDefaultClaudeHome)
    if (-not (Test-Path -LiteralPath $live)) { throw "No settings.json at $live" }

    $settings = Read-CcsJson -Path $live
    if ((Test-CcsBedrockSettings -Settings $settings) -and -not $Force) {
        throw "~/.claude/settings.json currently holds Bedrock settings, not your personal ones. Refusing to overwrite the baseline (use -Force to override)."
    }

    $baseline = "$live.personal.bak"
    if ($PSCmdlet.ShouldProcess($baseline, 'Save personal settings baseline')) {
        Copy-Item -LiteralPath $live -Destination $baseline -Force
        Write-Verbose "Saved $baseline"
    }
    $baseline
}

function Invoke-CcsAdopt {
    <#
    .SYNOPSIS
        Move company settings out of ~/.claude into the work profile, restore your own.
    .DESCRIPTION
        Run this after the company install.bat. It captures whatever Bedrock
        settings the installer left in ~/.claude into the named profile, then puts
        your personal settings back so the default profile stays subscription-based.
    .PARAMETER Name
        Profile to receive the company settings. Defaults to 'work'.
    .PARAMETER KeepLive
        Leave ~/.claude/settings.json as-is instead of restoring the personal baseline.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [string]$Name = 'work',
        [switch]$KeepLive
    )

    $live = Get-CcsSettingsPath -ProfileDir (Get-CcsDefaultClaudeHome)
    $settings = Read-CcsJson -Path $live
    if (-not $settings) { throw "No readable settings.json at $live" }

    if (-not (Test-CcsBedrockSettings -Settings $settings)) {
        throw "~/.claude/settings.json is not a Bedrock configuration — run the company install.bat first (and answer 'y' when it offers to overwrite settings)."
    }

    if (-not $PSCmdlet.ShouldProcess($Name, 'Adopt company settings into profile')) { return }

    $captured = New-CcsProfileFromCurrent -Name $Name -Force -Note 'Company Bedrock (CCWB)' -SettingsOnly
    Write-Verbose "Captured company settings into $($captured.Dir)"

    $restored = $false
    if (-not $KeepLive) {
        $baseline = "$live.personal.bak"
        if (Test-Path -LiteralPath $baseline) {
            Copy-Item -LiteralPath $baseline -Destination $live -Force
            $restored = $true
        } else {
            Write-Warning "No personal baseline at $baseline — ~/.claude keeps the company settings. Restore your own settings, then run: ccs backup-personal"
        }
    }

    [pscustomobject]@{
        Profile           = $captured.Name
        Dir               = $captured.Dir
        Backend           = $captured.Backend
        PersonalRestored  = $restored
        Findings          = @(Test-CcsProfileHealth -Name $captured.Name)
    }
}