#!/usr/bin/env ruby
# Adds swift-snapshot-testing SPM dependency to the HoneyDueTests target.
# Idempotent — safe to run repeatedly.
#
# Usage:
#   ruby scripts/add_snapshot_testing.rb
#
# Requires:
#   gem install xcodeproj
#
# Repo-root-relative; must be run from repo root or via absolute path.

require 'xcodeproj'

PROJECT_PATH = File.expand_path('../iosApp/honeyDue.xcodeproj', __dir__)
PACKAGE_URL  = 'https://github.com/pointfreeco/swift-snapshot-testing'
PACKAGE_VERSION = '1.17.0'
PRODUCT     = 'SnapshotTesting'
TARGET_NAME = 'HoneyDueTests'

project = Xcodeproj::Project.open(PROJECT_PATH)
target  = project.targets.find { |t| t.name == TARGET_NAME }
abort("Target #{TARGET_NAME} not found") unless target

# 1. XCRemoteSwiftPackageReference — create if missing
pkg_ref = project.root_object.package_references.find do |ref|
  ref.respond_to?(:repositoryURL) && ref.repositoryURL == PACKAGE_URL
end

if pkg_ref.nil?
  pkg_ref = project.new(Xcodeproj::Project::Object::XCRemoteSwiftPackageReference)
  pkg_ref.repositoryURL = PACKAGE_URL
  pkg_ref.requirement = { 'kind' => 'upToNextMajorVersion', 'minimumVersion' => PACKAGE_VERSION }
  project.root_object.package_references << pkg_ref
  puts "Added XCRemoteSwiftPackageReference: #{PACKAGE_URL}"
else
  pkg_ref.requirement = { 'kind' => 'upToNextMajorVersion', 'minimumVersion' => PACKAGE_VERSION }
  puts "XCRemoteSwiftPackageReference already exists: #{PACKAGE_URL}"
end

# 2. XCSwiftPackageProductDependency — SnapshotTesting product tied to package ref
prod_dep = target.package_product_dependencies.find { |d| d.product_name == PRODUCT }

if prod_dep.nil?
  prod_dep = project.new(Xcodeproj::Project::Object::XCSwiftPackageProductDependency)
  prod_dep.package = pkg_ref
  prod_dep.product_name = PRODUCT
  target.package_product_dependencies << prod_dep
  puts "Added XCSwiftPackageProductDependency: #{PRODUCT} -> #{TARGET_NAME}"
else
  puts "XCSwiftPackageProductDependency #{PRODUCT} already linked to #{TARGET_NAME}"
end

# 3. Frameworks build phase — add build file referencing the product dep
frameworks_phase = target.frameworks_build_phase
already_linked = frameworks_phase.files.any? do |bf|
  bf.file_ref.nil? && bf.product_ref == prod_dep
end

unless already_linked
  build_file = project.new(Xcodeproj::Project::Object::PBXBuildFile)
  build_file.product_ref = prod_dep
  frameworks_phase.files << build_file
  puts "Added #{PRODUCT} to Frameworks build phase of #{TARGET_NAME}"
else
  puts "#{PRODUCT} already in Frameworks build phase"
end

project.save
puts "Saved project."
