name: Gitea CI/CD

on:
  workflow_dispatch:
  #push:
    #branches: [ "development", "main", "staging"]
    #tags: [ "v*.*.*" ]
  pull_request:
    #branches: [ "development", "main", "staging" ]
    tags: [ "v*.*.*" ]
    
permissions:
  contents: write
  packages: write

env:
  DOTNET_VERSION: '8.0.x'
  DOCKER_IMAGE_NAME: watcher-server
  REGISTRY_URL: git.triggermeelmo.com
  DOCKER_PLATFORMS: 'linux/amd64,linux/arm64'

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup .NET SDK
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: Restore dependencies
        run: dotnet restore

      - name: Build
        run: dotnet build --configuration Release --no-restore

      - name: Test
        run: dotnet test --no-build --verbosity normal
        continue-on-error: true

      - name: Publish
        run: dotnet publish -c Release -o out

  set-tag:
    name: Set Tag Name
    runs-on: ubuntu-latest
    outputs:
      tag_name: ${{ steps.set_tag.outputs.tag_name }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Determine next semantic version tag
        id: set_tag
        run: |
          git fetch --tags

          # Find latest tag matching vX.Y.Z
          latest_tag=$(git tag --list 'v*.*.*' --sort=-v:refname | head -n 1)
          if [[ -z "$latest_tag" ]]; then
            major=0
            minor=0
            patch=0
          else
            version="${latest_tag#v}"
            IFS='.' read -r major minor patch <<< "$version"
          fi

          if [[ "${GITHUB_REF}" == "refs/heads/main" ]]; then
            major=$((major + 1))
            minor=0
            patch=0
          elif [[ "${GITHUB_REF}" == "refs/heads/development" ]]; then
            minor=$((minor + 1))
            patch=0
          elif [[ "${GITHUB_REF}" == "refs/heads/staging" ]]; then
            patch=$((patch + 1))
          fi

          new_tag="v${major}.${minor}.${patch}"
          echo "Creating new version tag: ${new_tag}"
          echo "tag_name=${new_tag}" >> $GITHUB_OUTPUT

  docker-build-and-push:
    runs-on: ubuntu-latest
    needs: [build-and-test, set-tag]
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to Gitea Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY_URL}}
          username: ${{ secrets.AUTOMATION_USERNAME }}
          password: ${{ secrets.AUTOMATION_PASSWORD }}

      - name: Build and Push Multi-Arch Docker Image
        run: |
          docker buildx build \
            --platform ${{ env.DOCKER_PLATFORMS }} \
            -t ${{ env.REGISTRY_URL }}/watcher/${{ env.DOCKER_IMAGE_NAME }}:${{ needs.set-tag.outputs.tag_name }} \
            --push .

  tag:
    name: Create Tag
    needs: [docker-build-and-push]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Check if tag already exists
        id: check_tag
        run: |
          git fetch --tags
          if git rev-parse "${{ needs.set-tag.outputs.tag_name }}" >/dev/null 2>&1; then
            echo "Tag ${{ needs.set-tag.outputs.tag_name }} already exists!"
            echo "exists=true" >> $GITHUB_OUTPUT
          else
            echo "Tag ${{ needs.set-tag.outputs.tag_name }} does not exist"
            echo "exists=false" >> $GITHUB_OUTPUT
          fi

      - name: Create and push tag
        if: steps.check_tag.outputs.exists == 'false'
        run: |
          echo "Creating new tag: ${{ needs.set-tag.outputs.tag_name }}"
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"
          git tag ${{ needs.set-tag.outputs.tag_name }}
          git push -u origin ${{ needs.set-tag.outputs.tag_name }}
          echo "Tag created and pushed successfully"

      - name: Skip tag creation
        if: steps.check_tag.outputs.exists == 'true'
        run: |
          echo "Skipping tag creation - ${{ needs.set-tag.outputs.tag_name }} already exists"