name: Package and Release Extension

on:
  workflow_dispatch:
  push:
    branches:
      - test

jobs:
  build-and-release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Get current date
        id: date
        run: |
          echo "DATE=$(date +'%Y.%m.%d. %H:%M')" >> $GITHUB_ENV
          echo "DATE_FOR_ZIP=$(date +'%Y%m%d-%H%M')" >> $GITHUB_ENV

      - name: Create ZIP file
        run: |
          zip -r "pre-firxa-${{ env.DATE_FOR_ZIP }}.zip" . -x "*.git*" "*.github*" "*.idea*"

      - name: Delete previous pre-release
        uses: dev-drprasad/delete-tag-and-release@v0.2.1
        with:
          delete_release: true
          tag_name: pre-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        continue-on-error: true

      - name: Create new pre-release
        id: create_release
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: pre-release
          release_name: Fejlesztői build
          body: |
            Ez egy kiadás előtti build, amely minden egyes commit után frissül!
            A build automatikusan készült ekkor: ${{ env.DATE }}
          draft: false
          prerelease: true

      - name: Upload ZIP to release
        if: steps.create_release.outputs.upload_url != ''
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./pre-firxa-${{ env.DATE_FOR_ZIP }}.zip
          asset_name: pre-firxa-${{ env.DATE_FOR_ZIP }}.zip
          asset_content_type: application/zip

  check-links:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Link Checker
        id: lychee
        uses: lycheeverse/lychee-action@v2
        with:
          fail: false
