# yaml-language-server: $schema=https://aka.ms/teams-toolkit/1.1.0/yaml.schema.json
# Visit https://aka.ms/teamsfx-v5.0-guide for details on this file
# Visit https://aka.ms/teamsfx-actions for details on actions
version: 1.1.0

provision:
{{#if activePlugins.fx-resource-aad-app-for-teams}}
  - uses: aadApp/create # Creates a new Microsoft Entra app to authenticate users if the environment variable that stores clientId is empty
    with:
      name: {{aadAppName}} # Note: when you run aadApp/update, the Microsoft Entra app name will be updated based on the definition in manifest. If you don't want to change the name, make sure the name in Microsoft Entra manifest is the same with the name defined here.
      generateClientSecret: true # If the value is false, the action will not generate client secret for you
      signInAudience: "AzureADMyOrg" # Authenticate users with a Microsoft work or school account in your organization's Microsoft Entra tenant (for example, single tenant).
    writeToEnvironmentFile: # Write the information of created resources into environment file for the specified environment variable(s).
      clientId: AAD_APP_CLIENT_ID
      clientSecret: SECRET_AAD_APP_CLIENT_SECRET # Environment variable that starts with `SECRET_` will be stored to the .env.{envName}.user environment file
      objectId: AAD_APP_OBJECT_ID
      tenantId: AAD_APP_TENANT_ID
      authority: AAD_APP_OAUTH_AUTHORITY
      authorityHost: AAD_APP_OAUTH_AUTHORITY_HOST
{{/if}}
  - uses: teamsApp/create # Creates a Teams app
    with:
      name: {{teamsAppName}} # Teams app name
    writeToEnvironmentFile:
      # Write the information of created resources into environment file for the specified environment variable(s).
      teamsAppId: TEAMS_APP_ID
{{#if activePlugins.fx-resource-bot}}
  - uses: botAadApp/create # Creates a new Microsoft Entra app for Bot Registration.
    with:
      name: {{appName}}
    writeToEnvironmentFile:
      botId: BOT_ID
      botPassword: SECRET_BOT_PASSWORD
  # Create or update the bot registration on dev.botframework.com
  - uses: botFramework/create
    with:
      botId: $\{{BOT_ID}}
      name: {{appName}}
      messagingEndpoint: $\{{BOT_ENDPOINT}}/api/messages
      description: ""
  - uses: file/createOrUpdateJsonFile
    with:
      target: ./appsettings.Development.json
      content:
        BOT_ID: $\{{BOT_ID}}
        BOT_PASSWORD: $\{{SECRET_BOT_PASSWORD}}
  {{#if activePlugins.fx-resource-aad-app-for-teams}}
        TeamsFx:
          Authentication:
            ClientId: $\{{AAD_APP_CLIENT_ID}}
            ClientSecret: $\{{SECRET_AAD_APP_CLIENT_SECRET}}
            OAuthAuthority: $\{{AAD_APP_OAUTH_AUTHORITY}}
  {{/if}}
{{/if}}
{{#if activePlugins.fx-resource-frontend-hosting}}
  - uses: script # Set env for local launch
    with:
      run:
        echo "::set-teamsfx-env {{placeholderMappings.tabDomain}}=localhost:44302";
        echo "::set-teamsfx-env {{placeholderMappings.tabEndpoint}}=https://localhost:44302";
        echo "::set-teamsfx-env {{placeholderMappings.tabIndexPath}}=#";
  {{#if activePlugins.fx-resource-aad-app-for-teams}}
  - uses: file/createOrUpdateJsonFile
    with:
      target: ./appsettings.Development.json
      content:
        TeamsFx:
          Authentication:
            ClientId: $\{{AAD_APP_CLIENT_ID}}
            ClientSecret: $\{{SECRET_AAD_APP_CLIENT_SECRET}}
            OAuthAuthority: $\{{AAD_APP_OAUTH_AUTHORITY}}
  {{/if}} 
{{/if}}
{{#if activePlugins.fx-resource-bot}}
  - uses: script # Set env for local launch
    name: Set {{placeholderMappings.botDomain}} for local launch
    with:
      run: echo "::set-teamsfx-env {{placeholderMappings.botDomain}}=$\{{BOT_DOMAIN}}"
{{/if}}
{{#if activePlugins.fx-resource-aad-app-for-teams}}
  - uses: aadApp/update # Apply the Microsoft Entra manifest to an existing Microsoft Entra app. Will use the object id in manifest file to determine which Microsoft Entra app to update.
    with:
      manifestPath: ./aad.manifest.json # Relative path to this file. Environment variables in manifest will be replaced before apply to Microsoft Entra app
      outputFilePath : ./build/aad.manifest.$\{{TEAMSFX_ENV}}.json
{{/if}}
  - uses: teamsApp/validateManifest # Validate using manifest schema
    with:
      manifestPath: ./appPackage/manifest.json # Path to manifest template
  - uses: teamsApp/zipAppPackage # Build Teams app package with latest env value
    with:
      manifestPath: ./appPackage/manifest.json # Path to manifest template
      outputZipPath: ./build/appPackage/appPackage.$\{{TEAMSFX_ENV}}.zip
      outputJsonPath: ./build/appPackage/manifest.$\{{TEAMSFX_ENV}}.json
  - uses: teamsApp/update # Apply the Teams app manifest to an existing Teams app in Teams Developer Portal. Will use the app id in manifest file to determine which Teams app to update.
    with:
      appPackagePath: ./build/appPackage/appPackage.$\{{TEAMSFX_ENV}}.zip # Relative path to this file. This is the path for built zip file.