# SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
#
# SPDX-License-Identifier: CC0-1.0

version: "3"

vars:
  VERSION:
    sh: git describe --long 2>/dev/null | sed 's/\([^-]*-g\)/r\1/;s/-/./g'
  GOOS:
    sh: go env GOOS
  GOARCH:
    sh: go env GOARCH

env:
  CGO_ENABLED: 0

tasks:
  default:
    desc: Run all checks
    cmds:
      - task: fmt
      - task: lint:fix
      - task: staticcheck
      - task: test
      - task: vuln
      - task: reuse

  fmt:
    cmds:
      - go install mvdan.cc/gofumpt@latest
      - gofumpt -l -w .

  lint:
    cmds:
      - golangci-lint run

  lint:fix:
    cmds:
      - golangci-lint run --fix

  staticcheck:
    cmds:
      - go install honnef.co/go/tools/cmd/staticcheck@latest
      - staticcheck ./...

  test:
    cmds:
      - go test -v ./...

  vuln:
    cmds:
      - go install golang.org/x/vuln/cmd/govulncheck@latest
      - govulncheck ./...

  reuse:
    desc: Lint licenses and copyright headers
    cmds:
      - reuse lint

  build:
    cmds:
      - go build -o lunatask-mcp-server -ldflags "-s -w -X main.version={{.VERSION}}" ./cmd
    generates:
      - lunatask-mcp-server

  install:
    cmds:
      - go install -ldflags "-s -w -X main.version={{.VERSION}}" ./cmd

  run:
    cmds:
      - go run -ldflags "-s -w -X main.version={{.VERSION}}" ./cmd {{.CLI_ARGS}}

  pack:
    desc: Pack lunatask-mcp-server with UPX
    cmds:
      - upx --best -qo lunatask-mcp-server.min lunatask-mcp-server
      - mv lunatask-mcp-server.min lunatask-mcp-server
    sources:
      - lunatask-mcp-server

  clean:
    desc: Remove build artifacts
    cmds:
      - rm -rf lunatask-mcp-server

  clean-all:
    desc: Remove build artifacts and config.toml
    cmds:
      - rm -rf lunatask-mcp-server config.toml

  release:
    desc: Interactive release workflow
    vars:
      BUMP:
        sh: gum choose "major" "minor" "patch" "prerelease"
      CURRENT_VERSION:
        sh: git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0"
      IS_CURRENT_PRERELEASE:
        sh: |
          current="{{.CURRENT_VERSION}}"
          if echo "$current" | grep -qE '\-[a-zA-Z]+\.[0-9]+$'; then
            echo "yes"
          else
            echo "no"
          fi
      IS_PRERELEASE:
        sh: |
          if [ "{{.BUMP}}" = "prerelease" ]; then
            echo "yes"
          else
            gum confirm "Create pre-release?" && echo "yes" || echo "no"
          fi
      PRERELEASE_SUFFIX:
        sh: |
          if [ "{{.BUMP}}" = "prerelease" ] && [ "{{.IS_CURRENT_PRERELEASE}}" = "yes" ]; then
            # Extract suffix from current version (e.g., v1.2.3-beta.0 -> beta)
            echo "{{.CURRENT_VERSION}}" | sed -E 's/.*-([a-zA-Z]+)\.[0-9]+$/\1/'
          elif [ "{{.IS_PRERELEASE}}" = "yes" ]; then
            gum input --placeholder "Enter pre-release suffix (e.g., beta, rc)"
          fi
      BASE_NEXT:
        sh: |
          if [ "{{.BUMP}}" = "prerelease" ] && [ "{{.IS_CURRENT_PRERELEASE}}" = "yes" ]; then
            # Extract base version from current prerelease (e.g., v1.2.3-beta.0 -> v1.2.3)
            echo "{{.CURRENT_VERSION}}" | sed -E 's/-[a-zA-Z]+\.[0-9]+$//'
          else
            svu {{.BUMP}}
          fi
      SUFFIX_VERSION:
        sh: |
          if [ "{{.IS_PRERELEASE}}" = "yes" ] && [ -n "{{.PRERELEASE_SUFFIX}}" ]; then
            if [ "{{.BUMP}}" = "prerelease" ] && [ "{{.IS_CURRENT_PRERELEASE}}" = "yes" ]; then
              # Increment the current prerelease number
              current_num=$(echo "{{.CURRENT_VERSION}}" | sed -E 's/.*-[a-zA-Z]+\.([0-9]+)$/\1/')
              echo $((current_num + 1))
            else
              # Find existing tags with this suffix and get the highest version number
              highest=$(git tag -l "{{.BASE_NEXT}}-{{.PRERELEASE_SUFFIX}}.*" | \
                sed 's/.*-{{.PRERELEASE_SUFFIX}}\.//' | \
                sort -n | tail -1)
              if [ -n "$highest" ]; then
                echo $((highest + 1))
              else
                echo 0
              fi
            fi
          fi
      NEXT:
        sh: |
          if [ "{{.IS_PRERELEASE}}" = "yes" ] && [ -n "{{.PRERELEASE_SUFFIX}}" ]; then
            echo "{{.BASE_NEXT}}-{{.PRERELEASE_SUFFIX}}.{{.SUFFIX_VERSION}}"
          else
            echo "{{.BASE_NEXT}}"
          fi
    prompt: "Release {{.NEXT}}?"
    preconditions:
      - sh: '[ $(git symbolic-ref --short HEAD) = "main" ] || [ $(git symbolic-ref --short HEAD) = "dev" ]'
        msg: Not on main or dev branch
      - sh: "[ $(git status --porcelain=2 | wc -l) = 0 ]"
        msg: "Git is dirty"
    cmds:
      - llm-tag {{.NEXT}}
      - git push soft {{.NEXT}}
      - go list -m git.secluded.site/lunatask-mcp-server@{{.NEXT}} > /dev/null
      - echo "Released {{.NEXT}} and notified module proxy"
