#compdef pm

_pm() {
  local line state

  _arguments -C \
    '--help[Display this message and exit.]' \
    '-h[Display this message and exit.]' \
    '--version[Display application version.]' \
    '1: :->cmds' \
    '*::arg:->args'

  case $state in
    cmds)
      local -a commands
      commands=(
        'agent:list recent coding-agent sessions'
        'pool:manage worktree pool'
        'project:manage projects'
        'repo:manage canonical repos under ~/.repos'
        'stacker:branch-stack tracking against pm'\''s pool'
        'cd:Change directory to a project, or one of its worktrees.'
        'check:Verify invariants across pool and projects.'
      )
      _describe -t commands 'command' commands
      ;;
    args)
      case $words[1] in
        agent)
          _arguments -C \
            '--help[Display this message and exit.]' \
            '-h[Display this message and exit.]' \
            '--version[Display application version.]' \
            '1: :->cmds' \
            '*::arg:->args'

          case $state in
            cmds)
              local -a commands
              commands=(
                'ls:List recent chat sessions across Claude / Codex / Cursor, per project.'
                'claude:Launch Claude Code in the pm project dir, forwarding extra args.'
                'codex:Launch Codex CLI in the pm project dir, forwarding extra args.'
                'cursor:Launch Cursor agent in the pm project dir, forwarding extra args.'
              )
              _describe -t commands 'command' commands
              ;;
            args)
              case $words[1] in
                ls)
                  _arguments \
                    '1:project name (defaults to current project)' \
                    '2:every project' \
                    '-p[project name (defaults to current project)]:p:_pm_projects' \
                    '--project[project name (defaults to current project)]:project:_pm_projects' \
                    '--all[every project]' \
                    '-n[-n]:n' \
                    '--limit[-n]:limit' \
                    '--agent[comma-separated subset\: claude,codex,cursor]:agent' \
                    '--json[--json]' \
                    '--resume[pick a session interactively and resume it]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                claude)
                  _arguments \
                    '*:ARGS' \
                    '-p[project name (defaults to current project)]:p:_pm_projects' \
                    '--project[project name (defaults to current project)]:project:_pm_projects' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                codex)
                  _arguments \
                    '*:ARGS' \
                    '-p[project name (defaults to current project)]:p:_pm_projects' \
                    '--project[project name (defaults to current project)]:project:_pm_projects' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                cursor)
                  _arguments \
                    '*:ARGS' \
                    '-p[project name (defaults to current project)]:p:_pm_projects' \
                    '--project[project name (defaults to current project)]:project:_pm_projects' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
              esac
              ;;
          esac
          ;;
        pool)
          _arguments -C \
            '--help[Display this message and exit.]' \
            '-h[Display this message and exit.]' \
            '--version[Display application version.]' \
            '1: :->cmds' \
            '*::arg:->args'

          case $state in
            cmds)
              local -a commands
              commands=(
                'add:Mint a new pool slot.'
                'ls:List pool slots with claim status, grouped by repo.'
              )
              _describe -t commands 'command' commands
              ;;
            args)
              case $words[1] in
                add)
                  _arguments \
                    '1:repo:_pm_repos' \
                    '--repo[--repo]:repo:_pm_repos' \
                    '--json[--json]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                ls)
                  _arguments \
                    '1:limit to one repo (default\: all repos)' \
                    '-r[limit to one repo (default\: all repos)]:r' \
                    '--repo[limit to one repo (default\: all repos)]:repo' \
                    '--json[--json]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
              esac
              ;;
          esac
          ;;
        project)
          _arguments -C \
            '--help[Display this message and exit.]' \
            '-h[Display this message and exit.]' \
            '--version[Display application version.]' \
            '1: :->cmds' \
            '*::arg:->args'

          case $state in
            cmds)
              local -a commands
              commands=(
                'create:Create a project (optionally with initial worktrees).'
                'delete:Delete a whole project.'
                'ls:List projects, grouped by project name.'
                'status:Show worktree health, PR state, stacker arms, and recent agent sessions.'
                'wt:manage worktrees within a project'
              )
              _describe -t commands 'command' commands
              ;;
            args)
              case $words[1] in
                create)
                  _arguments \
                    '1:--project' \
                    '--project[--project]:project' \
                    '--wt[--wt]:wt' \
                    '--json[--json]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                delete)
                  _arguments \
                    '1:project:_pm_projects' \
                    '--project[--project]:project:_pm_projects' \
                    '--dry-run[--dry-run]' \
                    '--no-dry-run[--dry-run]' \
                    '--json[--json]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                ls)
                  _arguments \
                    '--json[--json]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                status)
                  _arguments \
                    '1:project name (defaults to current project)' \
                    '-p[project name (defaults to current project)]:p:_pm_projects' \
                    '--project[project name (defaults to current project)]:project:_pm_projects' \
                    '--json[--json]' \
                    '-s[comma-separated subset\: worktrees,prs,stacker,sessions]:s' \
                    '--section[comma-separated subset\: worktrees,prs,stacker,sessions]:section' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                wt)
                  _arguments -C \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]' \
                    '1: :->cmds' \
                    '*::arg:->args'

                  case $state in
                    cmds)
                      local -a commands
                      commands=(
                        'add:Add worktree(s) to an existing project.'
                        'attach:Re-attach worktrees (best-effort slot reclaim).'
                        'detach:Detach worktrees\: unlink forward + release pool row.'
                        'remove:Remove worktree row(s) from a project.'
                      )
                      _describe -t commands 'command' commands
                      ;;
                    args)
                      case $words[1] in
                        add)
                          _arguments \
                            '1:repo:_pm_repos' \
                            '2:project name (defaults to current project)' \
                            '--spec[--spec]:spec' \
                            '-p[project name (defaults to current project)]:p:_pm_projects' \
                            '--project[project name (defaults to current project)]:project:_pm_projects' \
                            '--json[--json]' \
                            '--help[Display this message and exit.]' \
                            '-h[Display this message and exit.]' \
                            '--version[Display application version.]'

                          ;;
                        attach)
                          _arguments \
                            '1:project name (defaults to current project)' \
                            '2:comma-separated worktree names' \
                            '3:every worktree in the project' \
                            '-p[project name (defaults to current project)]:p:_pm_projects' \
                            '--project[project name (defaults to current project)]:project:_pm_projects' \
                            '--wt[comma-separated worktree names]:wt:_pm_wt_attachable' \
                            '--all[every worktree in the project]' \
                            '--no-branch[--no-branch]' \
                            '--json[--json]' \
                            '--help[Display this message and exit.]' \
                            '-h[Display this message and exit.]' \
                            '--version[Display application version.]'

                          ;;
                        detach)
                          _arguments \
                            '1:project name (defaults to current project)' \
                            '2:comma-separated worktree names' \
                            '3:every worktree in the project' \
                            '-p[project name (defaults to current project)]:p:_pm_projects' \
                            '--project[project name (defaults to current project)]:project:_pm_projects' \
                            '--wt[comma-separated worktree names]:wt:_pm_wt_detachable' \
                            '--all[every worktree in the project]' \
                            '--dry-run[--dry-run]' \
                            '--no-dry-run[--dry-run]' \
                            '--json[--json]' \
                            '--help[Display this message and exit.]' \
                            '-h[Display this message and exit.]' \
                            '--version[Display application version.]'

                          ;;
                        remove)
                          _arguments \
                            '1:project name (defaults to current project)' \
                            '2:comma-separated worktree names' \
                            '3:every worktree in the project' \
                            '-p[project name (defaults to current project)]:p:_pm_projects' \
                            '--project[project name (defaults to current project)]:project:_pm_projects' \
                            '--wt[comma-separated worktree names]:wt:_pm_wt_all' \
                            '--all[every worktree in the project]' \
                            '--dry-run[--dry-run]' \
                            '--no-dry-run[--dry-run]' \
                            '--json[--json]' \
                            '--help[Display this message and exit.]' \
                            '-h[Display this message and exit.]' \
                            '--version[Display application version.]'

                          ;;
                      esac
                      ;;
                  esac
                  ;;
              esac
              ;;
          esac
          ;;
        repo)
          _arguments -C \
            '--help[Display this message and exit.]' \
            '-h[Display this message and exit.]' \
            '--version[Display application version.]' \
            '1: :->cmds' \
            '*::arg:->args'

          case $state in
            cmds)
              local -a commands
              commands=(
                'ls:List canonical repos with branch + upstream status.'
                'maintenance:Warm git object store + index + fsmonitor across repos and pool slots.'
                'pull:Fetch + ff-only pull each canonical repo.'
              )
              _describe -t commands 'command' commands
              ;;
            args)
              case $words[1] in
                ls)
                  _arguments \
                    '--offline[--offline]' \
                    '--json[--json]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                maintenance)
                  _arguments \
                    '-r[comma-separated repo list (defaults to every repo)]:r' \
                    '--repo[comma-separated repo list (defaults to every repo)]:repo' \
                    '--json[--json]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                pull)
                  _arguments \
                    '-r[comma-separated repo list (defaults to every repo)]:r' \
                    '--repo[comma-separated repo list (defaults to every repo)]:repo' \
                    '--json[--json]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
              esac
              ;;
          esac
          ;;
        stacker)
          _arguments -C \
            '--help[Display this message and exit.]' \
            '-h[Display this message and exit.]' \
            '--version[Display application version.]' \
            '1: :->cmds' \
            '*::arg:->args'

          case $state in
            cmds)
              local -a commands
              commands=(
                'abort:Abort a paused stacker operation and reset state.'
                'absorb:Cherry-pick the current branch'\''s new commits onto its parent.'
                'config:Get/set per-repo stacker config.'
                'continue:Resume a paused stacker operation.'
                'create:Create or adopt a tracked branch (optionally in a pool slot).'
                'guard:internal guardrails for Git hooks'
                'log:Show commits since the branch'\''s managed base.'
                'ls:Render the stack tree.'
                'pr:manage the cached PR association for tracked branches'
                'push:Force-push + create/update PRs for a scope.'
                'remove:Untrack a branch (optionally delete it); reparent children.'
                'rename:Rename the current branch.'
                'repair:Reset stored managed-base / last-synced / last-clean-head to match git.'
                'reparent:Move current branch onto a new parent; cherry-pick descendants.'
                'split:Move commits \[..HEAD\] onto a new child branch.'
                'sync:Cherry-pick branches onto their parents.'
              )
              _describe -t commands 'command' commands
              ;;
            args)
              case $words[1] in
                abort)
                  _arguments \
                    '1:repo name (defaults to the cwd'\''s pm slot)' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                absorb)
                  _arguments \
                    '1:--branch' \
                    '2:repo name (defaults to the cwd'\''s pm slot)' \
                    '--branch[--branch]:branch' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '--continue[resume a paused absorb]' \
                    '--abort[abort the paused op]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                config)
                  _arguments \
                    '1:--key' \
                    '2:--value' \
                    '--key[--key]:key' \
                    '--value[--value]:value' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '--list[list all set keys]' \
                    '--unset[remove a key]' \
                    '--json[--json]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                continue)
                  _arguments \
                    '1:repo name (defaults to the cwd'\''s pm slot)' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                create)
                  _arguments \
                    '1:--branch' \
                    '--branch[--branch]:branch' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '--on[--on]:on' \
                    '--copy[--copy]:copy' \
                    '--replace[--replace]' \
                    '--no-checkout[--no-checkout]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                guard)
                  _arguments -C \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]' \
                    '1: :->cmds' \
                    '*::arg:->args'

                  case $state in
                    cmds)
                      local -a commands
                      commands=(
                        'no-rebase:Exit nonzero when cwd is a stacker-tracked branch.'
                      )
                      _describe -t commands 'command' commands
                      ;;
                    args)
                      case $words[1] in
                        no-rebase)
                          _arguments \
                            '--help[Display this message and exit.]' \
                            '-h[Display this message and exit.]' \
                            '--version[Display application version.]'

                          ;;
                      esac
                      ;;
                  esac
                  ;;
                log)
                  _arguments \
                    '1:--branch' \
                    '2:repo name (defaults to the cwd'\''s pm slot)' \
                    '--branch[--branch]:branch' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                ls)
                  _arguments \
                    '1:--branch' \
                    '2:repo name (defaults to the cwd'\''s pm slot)' \
                    '3:every tracked branch in the repo (default\: current lineage)' \
                    '4:exclude ancestors from the walk' \
                    '5:exclude descendants from the walk' \
                    '6:start the walk from this branch' \
                    '--branch[--branch]:branch' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '-a[every tracked branch in the repo (default\: current lineage)]' \
                    '--all[every tracked branch in the repo (default\: current lineage)]' \
                    '--skip-ancestors[exclude ancestors from the walk]' \
                    '--skip-descendants[exclude descendants from the walk]' \
                    '--from[start the walk from this branch]:from' \
                    '-c[narrow to the current branch'\''s lineage (default\: all)]' \
                    '--current[narrow to the current branch'\''s lineage (default\: all)]' \
                    '--details[--details]:details:(none status status-counts all)' \
                    '--json[--json]' \
                    '--icons[--icons]' \
                    '--no-icons[--icons]' \
                    '--merged[--merged]' \
                    '--no-merged[--merged]' \
                    '--merged-style[--merged-style]:merged-style:(dimmed strikethrough normal)' \
                    '--color-mode[--color-mode]:color-mode:(off icon title full)' \
                    '--legend[--legend]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                pr)
                  _arguments -C \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]' \
                    '1: :->cmds' \
                    '*::arg:->args'

                  case $state in
                    cmds)
                      local -a commands
                      commands=(
                        'refresh:Look up the current GitHub PR for a tracked branch and cache it.'
                        'unlink:Clear the cached PR association for a branch (or --all branches).'
                      )
                      _describe -t commands 'command' commands
                      ;;
                    args)
                      case $words[1] in
                        refresh)
                          _arguments \
                            '1:--branch' \
                            '2:repo name (defaults to the cwd'\''s pm slot)' \
                            '--branch[--branch]:branch' \
                            '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                            '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                            '--help[Display this message and exit.]' \
                            '-h[Display this message and exit.]' \
                            '--version[Display application version.]'

                          ;;
                        unlink)
                          _arguments \
                            '1:--branch' \
                            '2:repo name (defaults to the cwd'\''s pm slot)' \
                            '--branch[--branch]:branch' \
                            '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                            '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                            '--all[clear PR links for every branch]' \
                            '--help[Display this message and exit.]' \
                            '-h[Display this message and exit.]' \
                            '--version[Display application version.]'

                          ;;
                      esac
                      ;;
                  esac
                  ;;
                push)
                  _arguments \
                    '1:--branch' \
                    '2:repo name (defaults to the cwd'\''s pm slot)' \
                    '3:every tracked branch in the repo (default\: current lineage)' \
                    '4:exclude ancestors from the walk' \
                    '5:exclude descendants from the walk' \
                    '6:start the walk from this branch' \
                    '--branch[--branch]:branch' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '-a[every tracked branch in the repo (default\: current lineage)]' \
                    '--all[every tracked branch in the repo (default\: current lineage)]' \
                    '--skip-ancestors[exclude ancestors from the walk]' \
                    '--skip-descendants[exclude descendants from the walk]' \
                    '--from[start the walk from this branch]:from' \
                    '--only[--only]' \
                    '--draft[--draft]' \
                    '--publish[--publish]' \
                    '--create-pr[--create-pr]' \
                    '--no-create-pr[--create-pr]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                remove)
                  _arguments \
                    '1:--branch' \
                    '2:repo name (defaults to the cwd'\''s pm slot)' \
                    '--branch[--branch]:branch' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '--force[--force]' \
                    '--parent[--parent]' \
                    '--keep-branch[--keep-branch]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                rename)
                  _arguments \
                    '1:--new-name' \
                    '--new-name[--new-name]:new-name' \
                    '--branch[--branch]:branch' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                repair)
                  _arguments \
                    '1:--base-ref' \
                    '--base-ref[--base-ref]:base-ref' \
                    '--branch[--branch]:branch' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                reparent)
                  _arguments \
                    '1:--new-parent' \
                    '--new-parent[--new-parent]:new-parent' \
                    '--branch[--branch]:branch' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '--continue[resume a paused cherry-pick]' \
                    '--abort[abort the paused op]' \
                    '--allow-drop-parent-modifications[drop out-of-band history changes below the stack range and replay only working …]' \
                    '--allow-drop-merge[when a descendant'\''s PR is merged but its squash is not on its parent, drop the …]' \
                    '--offline[skip the PR-state refresh; use cached pr_state for the merged-PR collapse.]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                split)
                  _arguments \
                    '1:--new-name' \
                    '2:--commit' \
                    '--new-name[--new-name]:new-name' \
                    '--commit[--commit]:commit' \
                    '--branch[--branch]:branch' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '--stay[--stay]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
                sync)
                  _arguments \
                    '1:--branch' \
                    '2:repo name (defaults to the cwd'\''s pm slot)' \
                    '3:every tracked branch in the repo (default\: current lineage)' \
                    '4:exclude ancestors from the walk' \
                    '5:exclude descendants from the walk' \
                    '6:start the walk from this branch' \
                    '--branch[--branch]:branch' \
                    '-r[repo name (defaults to the cwd'\''s pm slot)]:r' \
                    '--repo[repo name (defaults to the cwd'\''s pm slot)]:repo:_pm_repos' \
                    '-a[every tracked branch in the repo (default\: current lineage)]' \
                    '--all[every tracked branch in the repo (default\: current lineage)]' \
                    '--skip-ancestors[exclude ancestors from the walk]' \
                    '--skip-descendants[exclude descendants from the walk]' \
                    '--from[start the walk from this branch]:from' \
                    '--continue[resume a paused cherry-pick]' \
                    '--abort[abort the paused op]' \
                    '--allow-drop-parent-modifications[drop out-of-band history changes below the stack range and replay only the bran…]' \
                    '--allow-drop-merge[when the branch'\''s PR is merged but the squash isn'\''t on the parent, drop the bra…]' \
                    '--offline[skip the PR-state refresh; use cached pr_state for the merged-PR collapse decis…]' \
                    '--help[Display this message and exit.]' \
                    '-h[Display this message and exit.]' \
                    '--version[Display application version.]'

                  ;;
              esac
              ;;
          esac
          ;;
        cd)
          _arguments \
            '1:project:_pm_projects' \
            '2:wt:_pm_wt_for_cd' \
            '--project[--project]:project:_pm_projects' \
            '--wt[--wt]:wt:_pm_wt_for_cd' \
            '--print[print the path instead of cd'\''ing (opts out of the shell wrapper)]' \
            '--help[Display this message and exit.]' \
            '-h[Display this message and exit.]' \
            '--version[Display application version.]'

          ;;
        check)
          _arguments \
            '--fix[--fix]' \
            '--no-fix[--fix]' \
            '--json[--json]' \
            '--help[Display this message and exit.]' \
            '-h[Display this message and exit.]' \
            '--version[Display application version.]'

          ;;
      esac
      ;;
  esac
}



# --- pm dynamic completion helpers (appended by pm __complete zsh-script) ---
# Cyclopts's generated script ends with `}` and no self-invocation. Under
# zsh-style autoload that means the first TAB only redefines _pm; the user
# would need a second TAB to trigger completion. Invoking _pm at the end
# of the file body (so on first load it runs once) fixes that.

_pm_extract_flag() {
  # _pm_extract_flag --flag [--alias ...]
  # Scan $words for the first matching flag; echo the value immediately
  # after it, or nothing.
  local flag i=2
  while (( i < ${#words} )); do
    for flag; do
      [[ $words[i] == $flag ]] && { print -r -- "$words[i+1]"; return 0; }
    done
    (( i++ ))
  done
  return 1
}

_pm_projects() {
  local -a items
  items=(${(f)"$(command pm __complete projects 2>/dev/null)"})
  _describe -t projects 'project' items
}

_pm_repos() {
  local -a items
  items=(${(f)"$(command pm __complete repos 2>/dev/null)"})
  _describe -t repos 'repo' items
}

_pm_wt_list() {
  # Comma-list completion for --wt. `_values -s ,` handles subtraction
  # of already-typed values automatically. $1 selects the state filter
  # (all | attached | unattached).
  local state=${1:-all}
  local project=$(_pm_extract_flag --project -p)
  local -a args items
  (( ${#project} )) && args=(--project "$project")
  items=(${(f)"$(command pm __complete worktrees --state $state $args 2>/dev/null)"})
  if (( ${#items} == 0 )); then
    # `_values` errors with "not enough arguments" if no candidates follow
    # the description; surface an inline hint instead of a hard error.
    _message -e worktrees "no $state worktrees for this project"
    return 1
  fi
  _values -s , 'worktree' $items
}
_pm_wt_attachable() { _pm_wt_list unattached }
_pm_wt_detachable() { _pm_wt_list attached }
_pm_wt_all()        { _pm_wt_list all }

_pm_wt_for_cd() {
  # `pm cd <project> <wt>` — read the project from the first positional
  # ($words[1]=pm, $words[2]=cd, $words[3]=<project>). Falls back to the
  # --project flag if someone wrote it that way.
  local project=${words[3]:-}
  if [[ -z "$project" || "$project" == --* || "$project" == -* ]]; then
    project=$(_pm_extract_flag --project -p)
  fi
  if [[ -z "$project" ]]; then
    _message -e worktrees "specify a project first"
    return 1
  fi
  local -a items
  items=(${(f)"$(command pm __complete worktrees --state all --project "$project" 2>/dev/null)"})
  if (( ${#items} == 0 )); then
    _message -e worktrees "no worktrees in project '$project'"
    return 1
  fi
  _describe -t worktrees 'worktree' items
}

_pm "$@"

