require 'xcodeproj'
require 'fileutils'

root = File.expand_path(__dir__)
project_path = File.join(root, 'Bookibra.xcodeproj')
FileUtils.rm_rf(project_path)

project = Xcodeproj::Project.new(project_path)
target = project.new_target(:application, 'Bookibra', :ios, '17.0')

project.build_configurations.each do |config|
  config.build_settings['SWIFT_VERSION'] = '5.9'
  config.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = 'com.bookibra.ios'
  config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '17.0'
  config.build_settings['TARGETED_DEVICE_FAMILY'] = '1,2'
  config.build_settings['CODE_SIGN_STYLE'] = 'Automatic'
  config.build_settings['MARKETING_VERSION'] = '1.0'
  config.build_settings['CURRENT_PROJECT_VERSION'] = '1'
end

target.build_configurations.each do |config|
  config.build_settings['SWIFT_VERSION'] = '5.9'
  config.build_settings['INFOPLIST_FILE'] = 'Bookibra/Resources/Info.plist'
  config.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = 'com.bookibra.ios'
  config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '17.0'
  config.build_settings['TARGETED_DEVICE_FAMILY'] = '1,2'
  config.build_settings['ASSETCATALOG_COMPILER_APPICON_NAME'] = 'AppIcon'
  config.build_settings['GENERATE_INFOPLIST_FILE'] = 'NO'
  config.build_settings['SWIFT_EMIT_LOC_STRINGS'] = 'YES'
  config.build_settings['LD_RUNPATH_SEARCH_PATHS'] = ['$(inherited)', '@executable_path/Frameworks']
end

# xcconfig mapping
config_map = {
  'Debug' => 'Bookibra/Resources/Debug.xcconfig',
  'Release' => 'Bookibra/Resources/Release.xcconfig'
}
project.build_configurations.each do |cfg|
  path = config_map[cfg.name]
  next unless path
  cfg.base_configuration_reference = project.files.find { |f| f.path == path } || project.main_group.new_file(path)
end
target.build_configurations.each do |cfg|
  path = config_map[cfg.name]
  next unless path
  cfg.base_configuration_reference = project.files.find { |f| f.path == path } || project.main_group.new_file(path)
end

main = project.main_group
bookibra_group = main.new_group('Bookibra', 'Bookibra')

# Source files
swift_files = Dir.glob(File.join(root, 'Bookibra', '**', '*.swift')).sort
swift_files.each do |abs|
  rel = abs.sub(root + '/', '')
  ref = bookibra_group.new_file(rel.sub('Bookibra/', ''))
  target.source_build_phase.add_file_reference(ref)
end

# Resource files/directories (only top-level bundles, no nested xcassets internals)
resource_paths = [
  'Bookibra/Resources/Assets.xcassets',
  'Bookibra/Resources/en.lproj/Localizable.strings',
  'Bookibra/Resources/tr.lproj/Localizable.strings',
  'Bookibra/Resources/mock_book_remote.json'
]
resource_paths.each do |rel|
  ref = bookibra_group.new_file(rel.sub('Bookibra/', ''))
  target.resources_build_phase.add_file_reference(ref)
end

# Frameworks
frameworks_group = main['Frameworks'] || main.new_group('Frameworks')
['AVFoundation.framework', 'VisionKit.framework', 'SwiftData.framework'].each do |framework|
  ref = frameworks_group.new_file("System/Library/Frameworks/#{framework}")
  target.frameworks_build_phase.add_file_reference(ref)
end

project.save
puts "Created #{project_path}"
