default_platform(:ios)

platform :ios do
  EXPORT_OPTIONS = {
    method: "app-store",
    signingStyle: "manual",
    manageAppVersionAndBuildNumber: false,
    provisioningProfiles: {
      "com.alexandrevazquez.PortfolioJournal" => "porfoliojournal",
      "com.alexandrevazquez.PortfolioJournal.PortfolioJournalWidget" => "Portfolio Journalwidget"
    }
  }

  def api_key
    app_store_connect_api_key(
      key_id: sh("pass show appstore/api-key-id", log: false).strip,
      issuer_id: sh("pass show appstore/issuer-id", log: false).strip,
      key_content: sh("pass show appstore/api-key-p8", log: false).strip,
      is_key_content_base64: false
    )
  end

  desc "Upload metadata and release notes only (binary already uploaded)"
  lane :metadata do
    deliver(
      api_key: api_key,
      metadata_path: "fastlane/metadata",
      skip_screenshots: true,
      skip_binary_upload: true,
      force: true
    )
  end

  desc "Push a new beta build to TestFlight"
  lane :beta do
    build_app(
      scheme: "PortfolioJournal",
      export_options: EXPORT_OPTIONS
    )
    upload_to_testflight(
      api_key: api_key,
      skip_waiting_for_build_processing: true
    )
  end

  desc "Submit already-uploaded build for App Store review (binary already in TestFlight)"
  lane :submit do
    deliver(
      api_key: api_key,
      metadata_path: "fastlane/metadata",
      skip_screenshots: true,
      skip_binary_upload: true,
      submit_for_review: true,
      automatic_release: false,
      run_precheck_before_submit: false,
      force: true
    )
  end

  desc "Build, export and push a new release to the App Store"
  lane :release do
    build_app(
      scheme: "PortfolioJournal",
      export_options: EXPORT_OPTIONS
    )
    upload_to_app_store(
      api_key: api_key,
      metadata_path: "fastlane/metadata",
      skip_screenshots: true,
      force: true
    )
  end
end
