# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

name: Native Libraries (Windows)

on:
  pull_request:
    branches:
      - main
    paths:
      - "adbc.h"
      - "c/**"
      - "ci/**"
      - "glib/**"
      - "go/**"
      - "python/**"
      - "ruby/**"
      - ".github/workflows/native-windows.yml"
  push:
    paths:
      - "adbc.h"
      - "c/**"
      - "ci/**"
      - "glib/**"
      - "go/**"
      - "python/**"
      - "ruby/**"
      - ".github/workflows/native-windows.yml"

concurrency:
  group: ${{ github.repository }}-${{ github.ref }}-${{ github.workflow }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  # ------------------------------------------------------------
  # Common build (builds libraries used in GLib, Python, Ruby)
  # ------------------------------------------------------------
  drivers-build-conda:
    name: "Common C/C++ Libraries (Conda/${{ matrix.os }})"
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: ["windows-latest"]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
          persist-credentials: false
      - name: Get Date
        id: get-date
        shell: bash
        run: |
          echo "today=$(/bin/date -u '+%Y%m%d')" >> $GITHUB_OUTPUT
      - name: Cache Conda
        uses: actions/cache@v3
        env:
          # Increment this to reset cache manually
          CACHE_NUMBER: 0
        with:
          path: ~/conda_pkgs_dir
          key: conda-${{ runner.os }}-${{ steps.get-date.outputs.today }}-${{ env.CACHE_NUMBER }}-${{ hashFiles('ci/**') }}
      - uses: conda-incubator/setup-miniconda@v2
        with:
          miniforge-variant: Mambaforge
          miniforge-version: latest
          use-mamba: true
      - name: Install Dependencies
        shell: bash -l {0}
        run: |
          mamba install -c conda-forge \
            --file ci/conda_env_cpp.txt
          # Force bundled gtest
          mamba uninstall gtest

      - name: Build and Install (No ASan)
        shell: pwsh
        env:
          BUILD_ALL: "1"
          # TODO(apache/arrow-adbc#634)
          BUILD_DRIVER_FLIGHTSQL: "0"
          BUILD_DRIVER_SNOWFLAKE: "0"
        run: |
          .\ci\scripts\cpp_build.ps1 $pwd ${{ github.workspace }}\build

      - uses: actions/upload-artifact@v3
        with:
          name: driver-manager-${{ matrix.os }}
          retention-days: 3
          path: |
            ${{ github.workspace }}/build

  # ------------------------------------------------------------
  # C/C++ build (builds and tests)
  # ------------------------------------------------------------
  drivers-test-conda:
    name: "C/C++ (Conda/${{ matrix.os }})"
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: ["windows-latest"]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
          persist-credentials: false
      - name: Get Date
        id: get-date
        shell: bash
        run: |
          echo "today=$(/bin/date -u '+%Y%m%d')" >> $GITHUB_OUTPUT
      - name: Cache Conda
        uses: actions/cache@v3
        env:
          # Increment this to reset cache manually
          CACHE_NUMBER: 0
        with:
          path: ~/conda_pkgs_dir
          key: conda-${{ runner.os }}-${{ steps.get-date.outputs.today }}-${{ env.CACHE_NUMBER }}-${{ hashFiles('ci/**') }}
      - uses: conda-incubator/setup-miniconda@v2
        with:
          miniforge-variant: Mambaforge
          miniforge-version: latest
          use-mamba: true
      - name: Install Dependencies
        shell: bash -l {0}
        run: |
          mamba install -c conda-forge \
            --file ci/conda_env_cpp.txt
          # Force bundled gtest
          mamba uninstall gtest

      - name: Build Driver Manager
        shell: pwsh
        env:
          BUILD_ALL: "0"
          BUILD_DRIVER_MANAGER: "1"
        run:
          .\ci\scripts\cpp_build.ps1 $pwd $pwd\build
      - name: Build Driver PostgreSQL
        shell: pwsh
        env:
          BUILD_ALL: "0"
          BUILD_DRIVER_POSTGRESQL: "1"
        run:
          .\ci\scripts\cpp_build.ps1 $pwd $pwd\build
      - name: Build Driver SQLite
        shell: pwsh
        env:
          BUILD_ALL: "0"
          BUILD_DRIVER_SQLITE: "1"
        run:
          .\ci\scripts\cpp_build.ps1 $pwd $pwd\build
      - name: Test Driver Manager
        shell: pwsh
        env:
          BUILD_ALL: "0"
          BUILD_DRIVER_MANAGER: "1"
        run:
          .\ci\scripts\cpp_test.ps1 $pwd $pwd\build
      - name: Test Driver SQLite
        shell: pwsh
        env:
          BUILD_ALL: "0"
          BUILD_DRIVER_SQLITE: "1"
        run:
          .\ci\scripts\cpp_test.ps1 $pwd $pwd\build

  # ------------------------------------------------------------
  # Go build
  # ------------------------------------------------------------
  go-conda:
    name: "Go (CGO) (Conda/${{ matrix.os }})"
    runs-on: ${{ matrix.os }}
    needs:
      - drivers-build-conda
    strategy:
      matrix:
        os: ["windows-latest"]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
          persist-credentials: false
      - name: Get Date
        id: get-date
        shell: bash
        run: |
          echo "today=$(/bin/date -u '+%Y%m%d')" >> $GITHUB_OUTPUT
      - name: Cache Conda
        uses: actions/cache@v3
        env:
          # Increment this to reset cache manually
          CACHE_NUMBER: 0
        with:
          path: ~/conda_pkgs_dir
          key: conda-${{ runner.os }}-${{ steps.get-date.outputs.today }}-${{ env.CACHE_NUMBER }}-${{ hashFiles('ci/**') }}
      - uses: conda-incubator/setup-miniconda@v2
        with:
          miniforge-variant: Mambaforge
          miniforge-version: latest
          use-mamba: true
      - name: Install Dependencies
        shell: bash -l {0}
        run: |
          mamba install -c conda-forge \
            --file ci/conda_env_cpp.txt
      - uses: actions/setup-go@v3
        with:
          go-version: 1.18.6
          check-latest: true
          cache: true
          cache-dependency-path: go/adbc/go.sum

      - uses: actions/download-artifact@v3
        with:
          name: driver-manager-${{ matrix.os }}
          path: ${{ github.workspace }}/build

      - name: Go Build
        shell: pwsh
        env:
          CGO_ENABLED: "1"
        run: |
          $env:PATH="$($env:RUNNER_TOOL_CACHE)\go\1.18.6\x64\bin;" + $env:PATH
          .\ci\scripts\go_build.ps1 $pwd $pwd\build
      # TODO(apache/arrow#358): enable these tests on Windows
      # - name: Go Test
      #   shell: pwsh
      #   env:
      #     CGO_ENABLED: "1"
      #   run: |
      #     $env:PATH="$($env:RUNNER_TOOL_CACHE)\go\1.18.6\x64\bin;" + $env:PATH
      #     .\ci\scripts\go_test.ps1 $pwd $pwd\build

  # ------------------------------------------------------------
  # Python build
  # ------------------------------------------------------------
  python-conda:
    name: "Python ${{ matrix.python }} (Conda/${{ matrix.os }})"
    runs-on: ${{ matrix.os }}
    needs:
      - drivers-build-conda
    strategy:
      matrix:
        os: ["windows-latest"]
        python: ["3.9", "3.11"]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
          persist-credentials: false
      - name: Get Date
        id: get-date
        shell: bash
        run: |
          echo "today=$(/bin/date -u '+%Y%m%d')" >> $GITHUB_OUTPUT
      - name: Cache Conda
        uses: actions/cache@v3
        env:
          # Increment this to reset cache manually
          CACHE_NUMBER: 0
        with:
          path: ~/conda_pkgs_dir
          key: conda-${{ runner.os }}-${{ steps.get-date.outputs.today }}-${{ env.CACHE_NUMBER }}-${{ hashFiles('ci/**') }}
      - uses: conda-incubator/setup-miniconda@v2
        with:
          miniforge-variant: Mambaforge
          miniforge-version: latest
          use-mamba: true
      - name: Install Dependencies
        shell: bash -l {0}
        run: |
          mamba install -c conda-forge \
            python=${{ matrix.python }} \
            --file ci/conda_env_cpp.txt \
            --file ci/conda_env_python.txt

      - uses: actions/download-artifact@v3
        with:
          name: driver-manager-${{ matrix.os }}
          path: ${{ github.workspace }}/build

      - name: Build Python Driver Manager
        shell: pwsh
        env:
          BUILD_ALL: "0"
          BUILD_DRIVER_MANAGER: "1"
        run:
          .\ci\scripts\python_build.ps1 $pwd $pwd\build
      - name: Build Python Driver PostgreSQL
        shell: pwsh
        env:
          BUILD_ALL: "0"
          BUILD_DRIVER_POSTGRESQL: "1"
        run:
          .\ci\scripts\python_build.ps1 $pwd $pwd\build
      - name: Build Python Driver SQLite
        shell: pwsh
        env:
          BUILD_ALL: "0"
          BUILD_DRIVER_SQLITE: "1"
        run:
          .\ci\scripts\python_build.ps1 $pwd $pwd\build
      - name: Test Python Driver Manager
        shell: pwsh
        env:
          BUILD_ALL: "0"
          BUILD_DRIVER_MANAGER: "1"
        run:
          .\ci\scripts\python_test.ps1 $pwd $pwd\build
      - name: Test Python Driver PostgreSQL
        shell: pwsh
        env:
          BUILD_ALL: "0"
          BUILD_DRIVER_POSTGRESQL: "1"
        run:
          .\ci\scripts\python_test.ps1 $pwd $pwd\build
      - name: Test Python Driver SQLite
        shell: pwsh
        env:
          BUILD_ALL: "0"
          BUILD_DRIVER_SQLITE: "1"
        run:
          .\ci\scripts\python_test.ps1 $pwd $pwd\build
