name: Go Linter

on:
  pull_request:
    branches:
      - master
      - release
  push:
    branches:
      - master
      - release

jobs:
  lint:
    if: 0
    name: Run golangci-lint
    runs-on: ubuntu-22.04

    strategy:
      matrix:
        project:
          - api_gateway

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: 1.24

      - name: Install golangci-lint
        run: |
          curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.62.2

      - name: Run golangci-lint
        run: |
          go mod tidy
          golangci-lint run -c ./.golangci-lint.yaml --fix=false --color=always

  build:
    if: 0
    name: Build golang services
    runs-on: ubuntu-22.04
    needs: lint

    strategy:
      matrix:
        project:
          - api_gateway

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: 1.24

      - name: Build golang services
        run: |
          go mod tidy
          CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o main cmd/main.go

  push:
    if: 0
    name: Push Docker Images
    runs-on: ubuntu-22.04
    needs: lint
    # if: ${{ gitea.ref == 'refs/heads/master' || gitea.ref == 'refs/heads/release' }}

    strategy:
      matrix:
        project:
          - api_gateway

    steps:
      - name: Checkout репозитория
        uses: actions/checkout@v4

      - name: Extract Git metadata
        id: git-metadata
        env:
          REPO: ${{ github.repository }}
        run: |
          GIT_SHA=$(git rev-parse --short HEAD)
          GIT_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo 'none')
          TARGET_ENV=${{ github.ref == 'refs/heads/release' && 'staging' || 'production' }}

          echo "GIT_SHA=${GIT_SHA}" >> $GITHUB_OUTPUT
          echo "GIT_TAG=${GIT_TAG}" >> $GITHUB_OUTPUT
          echo "TARGET_ENV=${TARGET_ENV}" >> $GITHUB_OUTPUT
          echo "REPO_NAME=$(echo $REPO | cut -d'/' -f2)" >> $GITHUB_OUTPUT

      - name: Login to Docker Registry
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract image metadata
        id: docker-metadata
        needs: git-metadata
        env:
          REGISTRY: hub.docker.com
          REPO_NAME: ${{ steps.git-metadata.outputs.REPO_NAME }}
          TARGET_ENV: ${{ steps.git-metadata.outputs.TARGET_ENV }}
          GIT_TAG: ${{ steps.git-metadata.outputs.GIT_TAG }}
          GIT_SHA: ${{ steps.git-metadata.outputs.GIT_SHA }}
        run: |
          IMAGE_TAG=$TARGET_ENV-$($GIT_TAG != 'none' && echo $GIT_TAG || echo $GIT_SHA)

          echo "IMAGE_NAME=molvaapp/${{ steps.git-metadata.outputs.REPO_NAME }}" >> $GITHUB_OUTPUT
          echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_OUTPUT

      - name: Build and Push Docker Image
        needs: docker-metadata
        env:
          IMAGE_NAME: ${{ steps.docker-metadata.outputs.IMAGE_NAME }}
          IMAGE_TAG: ${{ steps.docker-metadata.outputs.IMAGE_TAG }}
          CONFIG_FILE: .build/config/${{ steps.git-metadata.outputs.TARGET_ENV }}.yaml
        run: |
          docker build \
            --build-arg BUILD_LDFLAGS="-w -s" \
            --build-arg CONFIG_FILE_PATH=$CONFIG_FILE \
            -t $IMAGE_NAME:$IMAGE_TAG \
            -t $IMAGE_NAME:latest \
            .

          docker tag $IMAGE_NAME:$IMAGE_TAG $IMAGE_NAME:latest

          docker push $IMAGE_NAME:$IMAGE_TAG
          docker push $IMAGE_NAME:latest

          echo "Pushed image $IMAGE_NAME:$IMAGE_TAG and :latest"

  tag:
    name: Tag Commit
    runs-on: ubuntu-22.04
    rules:
      - if: github.event_name == 'push' && github.ref_type == 'tag'
        when: never
    # needs: lint

    steps:
      - name: Checkout репозитория
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Get latest tag
        id: get-latest-tag
        run: |
          LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo 'v0.0.0')
          echo $LATEST_TAG

          if [[ $LATEST_TAG =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
            MAJOR=${BASH_REMATCH[1]}
            MINOR=${BASH_REMATCH[2]}
            PATCH=${BASH_REMATCH[3]}
          else
            MAJOR=0
            MINOR=0
            PATCH=0
          fi

          echo $MAJOR.$MINOR.$PATCH

          NEW_PATCH=$((PATCH + 1))
          NEW_TAG="v${MAJOR}.${MINOR}.${NEW_PATCH}"

          echo "NEW_TAG=${NEW_TAG}" >> $GITHUB_OUTPUT

      - name: Push tag
        run: |
          git tag ${{ steps.get-latest-tag.outputs.NEW_TAG }}
          git push origin ${{ steps.get-latest-tag.outputs.NEW_TAG }}
