#requires -Version 5.1
[CmdletBinding(DefaultParameterSetName = 'Sync')]
param(
    [Parameter(ParameterSetName = 'Check')]
    [switch] $Check,

    [Parameter(ParameterSetName = 'Sync')]
    [switch] $AllowCopy,

    # Снимает junction'ы перед переключением на ветку/коммит без plugin/skills:
    # иначе git checkout падает на середине, не сумев создать файлы внутри битой ссылки.
    [Parameter(ParameterSetName = 'Remove')]
    [switch] $Remove
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

Import-Module (Join-Path $PSScriptRoot 'AgentAssets.psm1') -Force

$repoRoot = Split-Path -Parent $PSScriptRoot
$skills = @('kompas-3d', 'kompas-fdm-design')
$targetRoots = @(
    (Join-Path $repoRoot '.claude\skills'),
    (Join-Path $repoRoot '.agents\skills')
)

$failed = $false
foreach ($root in $targetRoots) {
    foreach ($skill in $skills) {
        $source = Join-Path $repoRoot "plugin\skills\$skill"
        $link = Join-Path $root $skill

        if ($Remove) {
            $state = Remove-AgentSkillLink -LinkPath $link
            if ($state -eq 'Skipped') {
                Write-Host "Skipped $link (настоящий каталог, не тронут)"
            }
            else {
                Write-Host "$state $link"
            }
            continue
        }

        if ($Check) {
            if (Test-AgentSkillLink -SourcePath $source -LinkPath $link) {
                Write-Host "ok    $link"
            }
            else {
                Write-Host "БИТО  $link"
                $failed = $true
            }
            continue
        }

        $state = Sync-AgentSkillLink -SourcePath $source -LinkPath $link -AllowCopy:$AllowCopy
        Write-Host "$state $link"
    }
}

if ($failed) { exit 1 }
