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

version: "3"

vars:
  VERSION:
    sh: git describe --tags --always 2>/dev/null || echo "v0.0.0"
  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
      - 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 ./...

  badge:
    desc: Update coverage badge
    cmds:
      - |
        pct=$(go test -cover ./... 2>&1 | grep -oP 'coverage: \K[0-9.]+')
        if [ "$pct" = "" ]; then echo "Failed to get coverage"; exit 1; fi
        color=red; [ "$(echo "$pct >= 50" | bc)" -eq 1 ] && color=yellow
        [ "$(echo "$pct >= 80" | bc)" -eq 1 ] && color=brightgreen
        sed -i "s|coverage-[0-9.]*%25-[a-z]*|coverage-${pct}%25-${color}|" README.md
        echo "Updated badge to ${pct}% (${color})"

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

  reuse:
    cmds:
      - reuse lint

  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/go-lunatask@{{.NEXT}} > /dev/null
      - echo "Released {{.NEXT}} and notified module proxy"
