# yaml-language-server: $schema=https://taskfile.dev/schema.json

# Copyright 2024 Google LLC. This software is provided as-is, without
# warranty or representation for any use or purpose. Your use of it is
# subject to your agreement with Google.
#
# Licensed 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.


version: "3"
output: 'prefixed'
tasks:
  
  ####################################
  # BUILD LOCAL BACKEND DOCKER IMAGE #
  ####################################
  build-local-backend:
    desc: Build the backend docker container
    dir: '../'
    preconditions:
      - sh: '[ -f "{{ .GCP_APPLICATION_DEFAULT_FILE_LOCATION }}" ]'
        msg: "Google Cloud credentials not found at {{ .GCP_APPLICATION_DEFAULT_FILE_LOCATION }}. Please run task gcloud:login"
    cmds:
      - |
        docker build \
        -t local-backend:latest \
        --secret id=gcp,src={{ .GCP_APPLICATION_DEFAULT_FILE_LOCATION }} \
        --file src/backend/local-be-docker-build \
        .
    run: once
    requires:
      vars: [GCP_APPLICATION_DEFAULT_FILE_LOCATION, RUN_CONTAINER_LOCALLY]
        # DEFAULT_HOST: localhost:8000
  
  #####################################
  # BUILD LOCAL FRONTEND DOCKER IMAGE #
  #####################################
  build-local-frontend:
    desc: Build the frontend docker container
    dir: '../'
    cmds:
      - |
        docker build \
        -t local-frontend:latest \
        --secret id=gcp,src={{ .GCP_APPLICATION_DEFAULT_FILE_LOCATION }} \
        --file src/frontend/local-fe-docker-build \
        ./src/frontend

    run: once
    requires:
      vars: [GCP_APPLICATION_DEFAULT_FILE_LOCATION]
  
  ######################################
  # RUN LOCAL BACKEND DOCKER CONTAINER #
  ######################################
  run-local-backend:
    desc: Run the local backend docker container
    deps: [stop-local-backend]
    cmds:
      - docker-compose up --build -d backend
    requires:
      vars:
        - GCP_APPLICATION_DEFAULT_FILE_LOCATION
        - RUN_CONTAINER_LOCALLY
    env:
      PORT: 8080
    vars:
      TGT_ADC_FILE: /home/vscode/.config/gcloud/application_default_credentials.json

  ####################################
  # RUN THE LOCAL FRONTEND CONTAINER #
  ####################################
  run-local-frontend:
    desc: Run the local frontend docker container
    deps: [stop-local-fe, build-local-frontend]
    cmds:
      - docker-compose up --build -d frontend
    env:
      PORT: 8000
  
  ##########################################
  # STOP A SPECIFIC CONTAINER FROM RUNNING #
  ##########################################
  stop-*:
    desc: Stop the defined container
    cmds:
      - cmd: docker-compose down
        ignore_error: true
    run: once
    status:
      - bash ./is_container_status.sh --stopped {{ .CONTAINER_NAME }}
    vars:
      CONTAINER_NAME: '{{ index .MATCH 0}}'

  #######################################################
  # RUN THE LOCAL FRONTEND AND LOCAL BACKEND CONTAINERS #
  #######################################################
  start-all:
    desc: Start all the containers
    cmds:
      - docker-compose up --build -d
      - docker ps

  ########################################################
  # STOP THE LOCAL FRONTEND AND LOCAL BACKEND CONTAINERS #
  ########################################################
  stop-all:
    desc: Stop all the containers
    cmds:
      - docker-compose down
      - docker ps
  
  #########################################################################
  # RUN A SEQUENCE OF DOCKER COMMANDS TO RESTART THE FRONTEND APPLICATION #
  #########################################################################
  restart-all:
    desc: Restart the entire application
    cmds:
      - task: stop-all
      - task: start-all
