name: release
on:
  push:
    tags:
      - 'v*'   # v1.2.3, v1.2.3-rc.1, etc.

jobs:
  build-and-publish-image:
    name: Build & Publish Image
    runs-on: [self-hosted, linux, x64, docker]   # NAS or any Linux runner with Docker
    env:
      REGISTRY: ${{ secrets.REGISTRY_HOST }}
      OWNER_REPO: ${{ github.repository }}
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-dotnet@v4
        with: { dotnet-version: '9.0.x' }

      - run: dotnet restore
      - run: dotnet publish JSMR/JSMR.Api/JSMR.Api.csproj -c Release -o publish

      - name: Create Dockerfile.ci
        run: |
          cat > Dockerfile.ci <<'EOF'
          FROM mcr.microsoft.com/dotnet/aspnet:9.0
          WORKDIR /app
          COPY publish/ ./
          ENV ASPNETCORE_URLS=http://+:8080
          EXPOSE 8080
          ENTRYPOINT ["dotnet", "JSMR.Api.dll"]
          EOF

      - name: Normalize image name
        id: names
        run: |
          IMAGE_LC="$(echo "${REGISTRY}/${OWNER_REPO}" | tr '[:upper:]' '[:lower:]')"
          echo "image=${IMAGE_LC}" >> "$GITHUB_OUTPUT"
          # tag derived from ref, e.g. refs/tags/v1.3.0 -> v1.3.0
          echo "tag=${GITHUB_REF_NAME}" >> "$GITHUB_OUTPUT"

      - name: Docker login
        run: |
          echo "${{ secrets.REGISTRY_PASSWORD }}" \
          | docker login "${{ env.REGISTRY }}" \
              -u "${{ secrets.REGISTRY_USER }}" \
              --password-stdin

      - name: Build image
        run: docker build -f Dockerfile.ci -t "${{ steps.names.outputs.image }}:${{ steps.names.outputs.tag }}" .

      - name: Push image
        run: |
          docker push "${{ steps.names.outputs.image }}:${{ steps.names.outputs.tag }}"
          docker tag "${{ steps.names.outputs.image }}:${{ steps.names.outputs.tag }}" "${{ steps.names.outputs.image }}:latest"
          docker push "${{ steps.names.outputs.image }}:latest"

      - uses: actions/upload-artifact@v4
        with:
          name: api-publish-${{ steps.names.outputs.tag }}
          path: publish

  create-gitea-release:
    name: Create Gitea Release
    needs: build-and-publish-image
    runs-on: [self-hosted, linux, x64, docker]
    steps:
      - uses: actions/checkout@v4

      # simplest notes: use tag message (annotated) and attach image refs
      - name: Generate basic notes
        id: notes
        run: |
          echo "## Release $GITHUB_REF_NAME" > REL_NOTES.md
          echo "" >> REL_NOTES.md
          echo "- Image: ${{ secrets.REGISTRY_HOST }}/${{ github.repository }}:${GITHUB_REF_NAME}" >> REL_NOTES.md
          echo "- Latest: ${{ secrets.REGISTRY_HOST }}/${{ github.repository }}:latest" >> REL_NOTES.md

      # Gitea has a create-release action too; if not, use API curl
      - name: Create Release via API
        env:
          GITEA_BASE: ${{ secrets.GITEA_BASE_URL }}    # e.g. https://gitea.example.com
          TOKEN: ${{ secrets.GITEA_TOKEN }}            # personal access token w/ repo write
        run: |
          BODY="$(cat REL_NOTES.md)"
          curl -sS -X POST "${GITEA_BASE}/api/v1/repos/${{ github.repository }}/releases" \
            -H "Authorization: token ${TOKEN}" \
            -H "Content-Type: application/json" \
            -d @- <<JSON
          {
            "tag_name": "${GITHUB_REF_NAME}",
            "name": "${GITHUB_REF_NAME}",
            "body": ${jq@JSON:- <<<'$BODY'},
            "draft": false,
            "prerelease": ${GITHUB_REF_NAME##*-rc*:+false}${GITHUB_REF_NAME##*-rc*:-true}
          }
          JSON
          
  deploy-api:
    name: Deploy API (Production)
    needs: build-and-publish-image
    runs-on: [self-hosted, linux, x64, docker]   # your Synology runner
    environment: production  # optional: add env protection rules in Gitea
    steps:
      - uses: actions/checkout@v4

      - name: Docker login (pull private image)
        run: |
          echo "${{ secrets.REGISTRY_PASSWORD }}" \
          | docker login "${{ secrets.REGISTRY_HOST }}" \
              -u "${{ secrets.REGISTRY_USER }}" \
              --password-stdin

      - name: Render env file for compose
        run: |
          mkdir -p deploy
          cat > deploy/.env <<EOF
          REGISTRY=${{ secrets.REGISTRY_HOST }}
          IMAGE_NS=${{ github.repository }}
          TAG=${GITHUB_REF_NAME}
          DB_CONN=${{ secrets.DB_CONNECTION_STRING }}
          ALLOWED_ORIGINS=${{ secrets.ALLOWED_ORIGINS }}
          TZ=America/Detroit
          EOF

      - name: Deploy (pull & up -d)
        run: |
          docker compose -f deploy/docker-compose.api.yml --env-file deploy/.env pull
          docker compose -f deploy/docker-compose.api.yml --env-file deploy/.env up -d
