var fastlaneTemplate = '''
default_platform(:ios)

APP_IDENTIFIER = "[APP_IDENTIFIER]"

platform :ios do
  private_lane :base_api_key do
    app_store_connect_api_key(
      key_id: "[API_KEY_ID]",
      issuer_id: "[API_ISSUER_ID]",
      key_content: "[AUTH_KEY_CONTENT]"
    )
  end


  desc "Track TestFlight status for a specific app version and build number (JSON output)"
  lane :testflight_status do
    require 'spaceship'
    require 'json'

    api_key = base_api_key

    target_app_version = "[TARGET_VERSION]"       # CFBundleShortVersionString
    target_build_number = "[TARGET_BUILD_NUMBER]"    # CFBundleVersion

    app = Spaceship::ConnectAPI::App.find(APP_IDENTIFIER)
    builds = app.get_builds(includes: "preReleaseVersion")

    matched = builds.find do |b|
      b.version.to_s == target_build_number &&
        b.respond_to?(:pre_release_version) &&
        b.pre_release_version&.version == target_app_version
    end

    result = {
      version: target_app_version,
      build_number: target_build_number,
      state: matched ? matched.processing_state : "NONE",
      uploaded: matched ? matched.uploaded_date.to_s : nil
    }

    puts "[RESULT]: #{JSON.generate(result)}"
  end


  desc "Get the latest build number for a specific version registered on TestFlight (JSON output)"
  lane :latest_build_number do
    require 'json'

    api_key = base_api_key

    target_version = "[LATEST_VERSION]"

    latest_build = app_store_build_number(
      api_key: api_key,
      app_identifier: APP_IDENTIFIER,
      version: target_version,
      live: false
    )

    result = {
      version: target_version,
      latest_build_number: latest_build
    }

    puts "[RESULT]: #{JSON.generate(result)}"
  end


  desc "Output App Store Connect version status as JSON based on filter conditions"
  lane :appstore_versions do
    require 'spaceship'
    require 'json'
    require 'rubygems' # for Gem::Version

    api_key = base_api_key

    app = Spaceship::ConnectAPI::App.find(APP_IDENTIFIER)

    versions = app.get_app_store_versions
      .select { |v| v.platform == "IOS" }
      .reject { |v| v.app_store_state == "REPLACED_WITH_NEW_VERSION" }

    # 1. Latest among READY_FOR_SALE
    latest_ready = versions
      .select { |v| v.app_store_state == "READY_FOR_SALE" }
      .max_by { |v| v.respond_to?(:created_date) ? v.created_date.to_s : "" }

    # 2. Lowest version among PREPARE_FOR_SUBMISSION
    prepare_versions = versions
      .select { |v| v.app_store_state == "PREPARE_FOR_SUBMISSION" }
      .sort_by { |v| Gem::Version.new(v.version_string) }

    lowest_prepare = prepare_versions.first

    # 3. Other status versions (excluding READY_FOR_SALE, PREPARE_FOR_SUBMISSION, REPLACED)
    included_states = ["READY_FOR_SALE", "PREPARE_FOR_SUBMISSION", "REPLACED_WITH_NEW_VERSION"]
    others = versions.reject { |v| included_states.include?(v.app_store_state) }

    selected = []
    selected << latest_ready if latest_ready
    selected << lowest_prepare if lowest_prepare
    selected += others

    json_output = selected.map do |v|
      {
        version: v.version_string,
        state: v.app_store_state,
        created: v.respond_to?(:created_date) ? v.created_date.to_s : nil
      }
    end

    puts "[RESULT]: #{JSON.generate(json_output)}"
  end
end
''';
