# This file contains the fastlane.tools configuration for the PediFoods iOS app.
# You can find the documentation at https://docs.fastlane.tools

default_platform(:ios)

lane :tests do
  run_tests(
    scheme: "PediFoods",
    project: "PediFoods.xcodeproj",
    clean: true,
    code_coverage: true
  )
end

lane :assemble do |options|
  # Manual signing (team/identity/profile) for the "PediFoods" target is
  # baked directly into PediFoods.xcodeproj's own project settings - not
  # overridden here.
  #
  # CURRENT_PROJECT_VERSION is likewise never overridden here - it's
  # whatever is committed in the project (manually bumped, e.g.
  # "2026.09.11.0.0.1"). A prior version of this lane overrode it with
  # the CI job's own run number (BUILD_NUMBER = github.run_number-style
  # counter) to guarantee a unique, ever-increasing build number per
  # run - but that silently replaced the real, meaningful version with
  # an unrelated small integer (a build uploaded as "51" instead of the
  # intended "2026.09.11.0.0.1"). Trusting the committed value means
  # re-running CI against the same commit without bumping
  # CURRENT_PROJECT_VERSION first will make TestFlight/App Store reject
  # the upload as a duplicate build number - a clear, safe failure,
  # which is the correct behavior for a manually-versioned project.
  xcargs = "-skipPackagePluginValidation -skipMacroValidation"

  build_app(
    scheme: "PediFoods",
    sdk: "iphoneos",
    export_method: "app-store",
    xcconfig: "fastlane/AppStore.xcconfig",
    xcargs: xcargs,
    derived_data_path: ".build/DerivedData",
    output_directory: ".build/fastlane",
    skip_archive: ENV["FASTLANE_SKIP_ARCHIVE"] == "YES",
    skip_codesigning: ENV["FASTLANE_SKIP_CODESIGNING"] == "YES"
  )
end

# Manual signing (PROVISIONING_PROFILE_SPECIFIER baked directly into
# PediFoods.xcodeproj's build settings, per target) means Xcode resolves
# a profile by matching that exact Name string against installed
# .mobileprovision files - not by bundle identifier alone. Without an
# explicit provisioning_name:, sigh names a freshly (re)created profile
# "<bundle identifier> AppStore" by default, which doesn't match the
# project's expected names ("LC Prov PediFoods Dist Profile" / "LC Prov
# PediFoods Dist Push Profile") - Xcode then can't find it and falls
# back to whatever stale profile happens to already be installed under
# the old name, which is exactly the doesn't-include-this-cert error
# force: true alone didn't fix. Also needs one call per target: sigh
# only touches the app_identifier it's given, and the
# NotificationServiceExtension has its own separate bundle id.
private_lane :renew_provisioning_profiles do
  get_provisioning_profile(
    api_key_path: "fastlane/apikey.json",
    app_identifier: "com.br.pedifoods.app",
    provisioning_name: "LC Prov PediFoods Dist Profile",
    force: true
  )
  get_provisioning_profile(
    api_key_path: "fastlane/apikey.json",
    app_identifier: "com.br.pedifoods.app.NotificationService",
    provisioning_name: "LC Prov PediFoods Dist Push Profile",
    force: true
  )
end

lane :beta do |options|
  desc "Build and upload to TestFlight"

  renew_provisioning_profiles

  assemble

  upload_to_testflight(
    api_key_path: "fastlane/apikey.json",
    skip_waiting_for_build_processing: true
  )
end

lane :release do |options|
  desc "Build and release app"

  # see https://docs.fastlane.tools/uploading-app-privacy-details/
  #upload_app_privacy_details_to_app_store(json_path: "fastlane/app_privacy_details.json")

  # if you have an apikey.json file (https://developer.apple.com/documentation/appstoreconnectapi/creating-api-keys-for-app-store-connect-api), fastlane can automatically fetch certificates and the ASC authentication information
  #get_certificates(api_key_path: "fastlane/apikey.json")
  renew_provisioning_profiles

  assemble

  upload_to_app_store(
    api_key_path: "fastlane/apikey.json",
    app_rating_config_path: "fastlane/metadata/rating.json",
    release_notes: { default: "Fixes and improvements." },
    submit_for_review: false
  )
end

