name: CI/CD

on:
  push:
    branches:
      - main
    tags:
      - 'v*'

jobs:
  check:
    name: Code Check
    runs-on: windows-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 9

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '24.14.0'
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install

      - name: Type check backend
        run: |
          cd backend
          pnpm exec tsc --noEmit

      - name: Type check frontend
        run: |
          cd front
          pnpm exec vue-tsc --noEmit

  build:
    name: Build Windows
    runs-on: windows-latest
    if: startsWith(github.ref, 'refs/tags/v')
    permissions:
      contents: write
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 9

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '24.14.0'
          cache: 'pnpm'

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Install dependencies
        run: pnpm install

      - name: Build backend
        run: pnpm run backend

      - name: Compress with UPX
        run: pnpm run upx

      - name: Copy backend to frontend
        run: pnpm run back2front

      - name: Build frontend
        run: pnpm run tauri

      - name: Move build to root
        run: pnpm run build2root

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: DeEarthX-V3-${{ github.ref_name }}-windows
          path: |
            DeEarthX-V3_x64-setup.exe
            DeEarthX-V3_x64-setup.zip

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            DeEarthX-V3_x64-setup.exe
            DeEarthX-V3_x64-setup.zip
          draft: false
          prerelease: false
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
