name: Debug Build and Test

on: [push, pull_request]

jobs:
  build-latest:
    name: Build against Latest Dalamud
    runs-on: windows-2022

    # Define the plugin name and Dalamud version variables for this job
    env:
      PLUGIN_NAME: CBT
      DALAMUD_VERSION_NAME: "Latest"
      DALAMUD_VERSION_URL: "https://goatcorp.github.io/dalamud-distrib/latest.zip"

    steps:
      # Checkout the repository code
      - name: Checkout and Initialise
        uses: actions/checkout@v4

      # Install the required .NET SDK
      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '9.x.x'

      # Cache the nuget packages.
      - name: Cache Dependencies
        id: cache-dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.nuget/packages
          key: ${{ runner.os }}-nuget-${{ hashFiles('**/${{ env.PLUGIN_NAME }}.csproj') }}
          restore-keys: |
            ${{ runner.os }}-nuget-

      # Create the required directory structure and download/extract Dalamud.
      - name: Download and extract Dalamud (${{ env.DALAMUD_VERSION_NAME }})
        run: |
          mkdir -p "$env:AppData\XIVLauncher\addon\Hooks\dev"
          Invoke-WebRequest -Uri "${{ env.DALAMUD_VERSION_URL }}" -OutFile "dalamud.zip"
          Expand-Archive -Path "dalamud.zip" -DestinationPath "$env:AppData\XIVLauncher\addon\Hooks\dev" -Force

      # Restore, build, and test.
      - name: Build Debug (${{ env.DALAMUD_VERSION_NAME }})
        id: build_step
        run: |
          dotnet restore `
            && dotnet build --no-restore --configuration Debug `
            && dotnet test --no-build --configuration Debug

      # Upload the build artifact. This step will only run if the build_step succeeded.
      - name: Upload Artifact (${{ env.DALAMUD_VERSION_NAME }})
        if: steps.build_step.outcome == 'success'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.PLUGIN_NAME }}-debug-${{ env.DALAMUD_VERSION_NAME }}-${{ github.sha }}
          path: |
            ${{ env.PLUGIN_NAME }}/bin/Debug/

  build-staging:
    name: Build against Staging Dalamud
    runs-on: windows-2022

    # Define the plugin name and Dalamud version variables for this job
    env:
      PLUGIN_NAME: CBT
      DALAMUD_VERSION_NAME: "Staging"
      DALAMUD_VERSION_URL: "https://goatcorp.github.io/dalamud-distrib/stg/latest.zip"

    steps:
      # Checkout the repository code
      - name: Checkout and Initialise
        uses: actions/checkout@v4

      # Install the required .NET SDK
      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '9.x.x'

      # Cache the nuget packages
      - name: Cache Dependencies
        id: cache-dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.nuget/packages
          key: ${{ runner.os }}-nuget-${{ hashFiles('**/${{ env.PLUGIN_NAME }}.csproj') }}
          restore-keys: |
            ${{ runner.os }}-nuget-

      # Create the required directory structure and download/extract Dalamud.
      - name: Download and extract Dalamud (${{ env.DALAMUD_VERSION_NAME }})
        run: |
          mkdir -p "$env:AppData\XIVLauncher\addon\Hooks\dev"
          Invoke-WebRequest -Uri "${{ env.DALAMUD_VERSION_URL }}" -OutFile "dalamud.zip"
          Expand-Archive -Path "dalamud.zip" -DestinationPath "$env:AppData\XIVLauncher\addon\Hooks\dev" -Force

      # Restore, build, and test.
      - name: Build Debug (${{ env.DALAMUD_VERSION_NAME }})
        id: build_step
        run: |
          dotnet restore `
            && dotnet build --no-restore --configuration Debug `
            && dotnet test --no-build --configuration Debug

      # Upload the build artifact.
      - name: Upload Artifact (${{ env.DALAMUD_VERSION_NAME }})
        if: steps.build_step.outcome == 'success'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.PLUGIN_NAME }}-debug-${{ env.DALAMUD_VERSION_NAME }}-${{ github.sha }}
          path: |
            ${{ env.PLUGIN_NAME }}/bin/Debug/