name: Build and Release Obsidian Plugin

on:
  push:
    branches:
      - master  # Запуск при пуше в master
    paths:
      - 'manifest.json'

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 18

      - name: Install dependencies
        run: npm install

      - name: Run tests
        run: npm test

      - name: Upload test coverage report
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: coverage/

  build:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 18

      - name: Install dependencies
        run: npm install

      - name: Build plugin
        run: npm run build

      - name: Create release folder
        run: |
          mkdir release
          cp main.js manifest.json styles.css release/

      - name: Upload built artifact
        uses: actions/upload-artifact@v4
        with:
          name: obsidian-plugin
          path: release/

  release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Download built artifact
        uses: actions/download-artifact@v4
        with:
          name: obsidian-plugin
          path: release

      - name: Get version from manifest.json
        id: get_version
        run: echo "VERSION=$(jq -r .version manifest.json)" >> $GITHUB_ENV

      - name: Create tag
        run: |
          git tag ${{ env.VERSION }}
          git push origin ${{ env.VERSION }}

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ env.VERSION }}
          files: release/*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
