name: Create Release Tag

on:
  push:
    tags:
      - 'v*.*.*'
  workflow_dispatch:
    inputs:
      tag:
        description: 'Tag to create (e.g. v1.2.3)'
        required: true
        type: string

permissions:
  contents: write

jobs:
  release:
    name: Create Release
    if: github.repository == 'vbenjs/vue-vben-admin'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v7
        with:
          fetch-depth: 0

      - name: Extract version
        id: version
        run: |
          if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
            raw_tag="${{ inputs.tag }}"
          else
            raw_tag="${GITHUB_REF_NAME}"
          fi
          # Normalize: ensure v prefix
          tag="${raw_tag}"
          [[ "${tag:0:1}" != "v" ]] && tag="v${tag}"
          version="${tag#v}"
          major="${version%%.*}"
          echo "tag=${tag}" >> "${GITHUB_OUTPUT}"
          echo "version=${version}" >> "${GITHUB_OUTPUT}"
          echo "major=${major}" >> "${GITHUB_OUTPUT}"

      - uses: release-drafter/release-drafter@v7
        with:
          version: ${{ steps.version.outputs.version }}
          publish: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
