steps:
  # Build Docker image with build args
  - name: 'gcr.io/cloud-builders/docker'
    args:
      - 'build'
      - '--target=runner'
      - '--build-arg=VITE_API_BASE_PATH=${_VITE_API_BASE_PATH}'
      - '--build-arg=VITE_USE_FAKE_BACKEND=${_VITE_USE_FAKE_BACKEND}'
      - '--build-arg=VITE_API_UNIFY_URL=${_VITE_API_UNIFY_URL}'
      - '-t'
      - 'asia-southeast1-docker.pkg.dev/$PROJECT_ID/ydm-c360-repo/frontend-admin:$BUILD_ID'
      - '-t'
      - 'asia-southeast1-docker.pkg.dev/$PROJECT_ID/ydm-c360-repo/frontend-admin:latest'
      - './frontend-admin'
    id: 'build-image'

  # Push to Artifact Registry
  - name: 'gcr.io/cloud-builders/docker'
    args:
      - 'push'
      - '--all-tags'
      - 'asia-southeast1-docker.pkg.dev/$PROJECT_ID/ydm-c360-repo/frontend-admin'
    id: 'push-image'

  # Deploy to Cloud Run
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: gcloud
    args:
      - 'run'
      - 'deploy'
      - '${_SERVICE_NAME}'
      - '--image=asia-southeast1-docker.pkg.dev/$PROJECT_ID/ydm-c360-repo/frontend-admin:$BUILD_ID'
      - '--region=${_REGION}'
      - '--platform=managed'
      - '--allow-unauthenticated'
      - '--port=8080'
      - '--memory=512Mi'
      - '--cpu=1'
      - '--timeout=300'
      - '--max-instances=100'
      - '--min-instances=0'
    id: 'deploy-cloud-run'

images:
  - 'asia-southeast1-docker.pkg.dev/$PROJECT_ID/ydm-c360-repo/frontend-admin:$BUILD_ID'
  - 'asia-southeast1-docker.pkg.dev/$PROJECT_ID/ydm-c360-repo/frontend-admin:latest'

options:
  machineType: 'E2_HIGHCPU_8'
  logging: CLOUD_LOGGING_ONLY

substitutions:
  _SERVICE_NAME: 'dev-c360-frontend-admin'
  _REGION: 'asia-southeast1'
  # API Configuration - Set once, used for all builds
  _VITE_API_BASE_PATH: 'https://dev-c360-management-api-907324191702.asia-southeast1.run.app'
  _VITE_USE_FAKE_BACKEND: 'false'
  _VITE_API_UNIFY_URL: 'https://dev-c360-management-api-907324191702.asia-southeast1.run.app'

timeout: '1200s'
