# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

platform :ios do
  before_all do
    setup_circle_ci
    app_store_api_key
  end

  desc 'Get the app store connect api key from the auth key'
  private_lane :app_store_api_key do
    key =
      app_store_connect_api_key(
        key_id: ENV['APP_STORE_CONNECT_KEY_ID'],
        issuer_id: ENV['APP_STORE_CONNECT_ISSUER_ID'],
        key_content: ENV['APP_STORE_CONNECT_API_KEY_B64'],
        is_key_content_base64: true,
        in_house: false,
      )

    key
  end

  devices = {
    "nona : Name of device id here" =>
      'device-id-here',
  }

  desc 'Download all certificates'
  lane :certificates do
    develop_certificates
    appstore_certificates
  end

  desc 'Get Latest Release Notes'
  lane :get_latest_release_notes do |options|
    input_path = options.fetch(:input_path, '../../latest-release-notes.txt')
    input_full_path = File.expand_path(input_path, File.dirname(__FILE__))
    input = File.read(input_full_path)
    input
  end

  desc 'Download or regenerate(with option) development and adhoc match certificates for ios signing'
  lane :develop_certificates do |options|
    readonly = options.fetch(:readonly, true)
    force_for_new_devices = options.fetch(:force_for_new_devices, false)
    match(readonly: readonly, force_for_new_devices: force_for_new_devices)
    match(
      type: 'adhoc',
      readonly: readonly,
      force_for_new_devices: force_for_new_devices,
    )
  end

  desc 'Download or regenerate(with option) appstore certificates for ios signing'
  lane :appstore_certificates do |options|
    readonly = options.fetch(:readonly, true)
    match(type: 'appstore', readonly: readonly)
  end

  desc 'Register new devices'
  lane :register_new_devices do
    register_devices(devices: devices)
    develop_certificates(readonly: false, force_for_new_devices: true)
    appstore_certificates
  end

  desc 'iOS build'
  lane :build do |options|
    build_config = options.fetch(:build_config, 'Release')
    app_identifier = options.fetch(:app_identifier, 'com.nonatemplate')
    match(type: 'appstore', app_identifier: app_identifier)

    package = load_json(json_path: '../package.json')
    build_number =
      latest_testflight_build_number(
        app_identifier: app_identifier,
        initial_build_number: 0,
      ) + 1

    version_number = package['version']
    increment_build_number(build_number: build_number)
    increment_version_number(version_number: version_number)

    build_app(
      scheme: 'nonatemplate',
      configuration: build_config,
      export_options: {
        method: 'appstore',
        provisioningProfiles: {
          app_identifier => "match AppStore #{app_identifier}",
        },
      },
    )

    version_number
  end

  desc 'iOS testflight deploy'
  lane :testflight_deploy do
    version_number = build(build_config: 'Release', app_identifier: 'com.nonatemplate')
    notes = get_latest_release_notes
    upload_to_testflight(app_identifier: 'com.nonatemplate', changelog: notes)

    slack(
      message:
        "New Testflight build(#{version_number}) available on Testflight",
    )
  end

  desc 'iOS production deploy'
  lane :production_deploy do
    version_number = build(build_config: 'Release', app_identifier: 'com.nonatemplate')

    upload_to_app_store(
      submit_for_review: false,
      force: true,
      automatic_release: false,
      app_review_attachment_file: './fastlane/review-info-videos.zip',
      screenshots_path: './fastlane/screenshots',
      metadata_path: './fastlane/metadata',
      precheck_include_in_app_purchases: false,
    )

    slack(
      message:
        "New Production build(#{version_number}) can be submitted for review on App Store Connect",
    )
  end

  error do |lane, exception|
    slack(
      message: 'An error has occurred',
      success: false,
      attachment_properties: {
        fields: [{ title: 'Details', value: exception.to_s }],
      },
      payload: {
        'Output' => exception.error_info.to_s,
      },
    )
  end
end
