name: Build and Deploy

on:
  push:
    branches:
      - master  # Change to your default branch if different

jobs:
  build:
    runs-on: self-hosted  # Ensure your self-hosted runner is configured

    steps:
    - name: Get Current User
      run: |
        $env:USERNAME
    
    - name: Checkout Repository
      uses: actions/checkout@v2

    - name: Set up .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: '8.0'  # Change to your required .NET version

    - name: Restore .NET Dependencies
      run: dotnet restore ./services/bytefy.image/bytefy.image/bytefy.image.csproj

    - name: Build .NET Project
      run: dotnet build --configuration Release ./services/bytefy.image/bytefy.image/bytefy.image.csproj

    - name: Publish .NET Project
      run: dotnet publish ./services/bytefy.image/bytefy.image/bytefy.image.csproj --configuration Release --output ./output/dotnet

    - name: Install Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '22.1.0'  # Change to your required Node.js version

    - name: Install Angular CLI
      run: npm install -g @angular/cli

    - name: Install Angular Dependencies
      run: |
        cd ./tools
        npm install --force

    - name: Build Angular App
      run: |
        cd ./tools
        ng build --configuration production --output-path=dist

    - name: Upload Artifacts
      uses: actions/upload-artifact@v3
      with:
        name: dotnet-artifacts
        path: ./output/dotnet

    - name: Upload Angular Artifacts
      uses: actions/upload-artifact@v3
      with:
        name: angular-artifacts
        path: ./tools/dist

  deploy:
    runs-on: self-hosted  # Ensure your self-hosted runner is configured
    needs: build

    steps:
    - name: Download .NET Artifacts
      uses: actions/download-artifact@v3
      with:
        name: dotnet-artifacts
        path: ./output/dotnet

    - name: Download Angular Artifacts
      uses: actions/download-artifact@v3
      with:
        name: angular-artifacts
        path: ./tools/dist

    - name: Check Output Files
      run: |
        dir ./output/dotnet
        dir ./tools/dist
    
    - name: Copy .NET Publish Files to IIS Server
      run: |
        xcopy ".\output\dotnet\*" "C:\inetpub\applications\bytefy.image" /s /i /y
        xcopy ".\tools\dist\*" "C:\inetpub\wwwroot\bytefy" /s /i /y

