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

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

default_platform(:ios)

skip_docs

desc "Increment the patch version number (e.g., 1.2.3 → 1.2.4)"
lane :bump_patch do
  ensure_git_status_clean

  version = increment_version_number_in_xcodeproj(bump_type: "patch")
  build_number = increment_build_number_in_xcodeproj

  commit_version_bump(message: "Bump patch: #{version} (#{build_number})")
  add_git_tag(tag: "build/#{version}-#{build_number}", sign: true)

  UI.success("Bumped patch version to #{version} (#{build_number})")
end

desc "Increment the minor version number (e.g., 1.2.3 → 1.3.0)"
lane :bump_minor do
  ensure_git_status_clean

  version = increment_version_number_in_xcodeproj(bump_type: "minor")
  build_number = increment_build_number_in_xcodeproj

  commit_version_bump(message: "Bump minor: #{version} (#{build_number})")
  add_git_tag(tag: "build/#{version}-#{build_number}", sign: true)

  UI.success("Bumped minor version to #{version} (#{build_number})")
end

desc "Increment the major version number (e.g., 1.2.3 → 2.0.0)"
lane :bump_major do
  ensure_git_status_clean

  version = increment_version_number_in_xcodeproj(bump_type: "major")
  build_number = increment_build_number_in_xcodeproj

  commit_version_bump(message: "Bump major: #{version} (#{build_number})")
  add_git_tag(tag: "build/#{version}-#{build_number}", sign: true)

  UI.success("Bumped major version to #{version} (#{build_number})")
end

desc "Set build number to a specific value (usage: fastlane set_build number:19)"
lane :set_build do |options|
  ensure_git_status_clean

  build_number = options[:number]
  UI.user_error!("Please provide a build number, e.g.: fastlane set_build number:19") unless build_number

  version = get_version_number_from_xcodeproj
  increment_build_number_in_xcodeproj(build_number: build_number.to_s)

  commit_version_bump(message: "Set build: #{version} (#{build_number})")
  add_git_tag(tag: "build/#{version}-#{build_number}", sign: true)

  UI.success("Set build number to #{build_number}")
end

desc "Increment build number by 1"
lane :bump_build do
  ensure_git_status_clean

  version = get_version_number_from_xcodeproj
  build_number = increment_build_number_in_xcodeproj

  commit_version_bump(message: "Bump build: #{version} (#{build_number})")
  add_git_tag(tag: "build/#{version}-#{build_number}", sign: true)

  UI.success("Bumped build number to #{build_number}")
end

desc "Prepare beta release: bump build, commit, tag, and push"
lane :beta do
  ensure_git_status_clean

  version = get_version_number_from_xcodeproj
  build_number = increment_build_number_in_xcodeproj

  commit_version_bump(message: "Bump build: #{version} (#{build_number})")
  add_git_tag(tag: "build/#{version}-#{build_number}", sign: true)
  push_to_git_remote

  UI.success("Finished beta tasks - #{version} (#{build_number})")
end

# desc "Prepare beta release with TestFlight build check: bump build, commit, tag, and push"
# lane :beta_testflight do
#   ensure_git_status_clean
#
#   version = get_version_number_from_xcodeproj
#   latest_build = latest_testflight_build_number
#
#   increment_build_number_in_xcodeproj(build_number: latest_build + 1)
#   build_number = get_build_number_from_xcodeproj
#
#   commit_version_bump
#   add_git_tag(sign: true)
#   push_to_git_remote
#
#   UI.success("Finished beta tasks - #{version} (#{build_number})")
# end
