require 'xcodeproj'
require 'fileutils'

project_path = 'CelestiaTrace.xcodeproj'

# Remove existing project if any
FileUtils.rm_rf(project_path)

# Create a new Xcode project
project = Xcodeproj::Project.new(project_path)

# Create groups
main_group = project.main_group
app_group = main_group.new_group('CelestiaTrace', 'CelestiaTrace')
tests_group = main_group.new_group('CelestiaTraceTests', 'Tests/CelestiaTraceTests')

# Helper to add files recursively
def add_files_recursively(dir, group, target, project)
  return unless File.directory?(dir)
  Dir.foreach(dir) do |entry|
    next if entry == '.' || entry == '..' || entry == '.DS_Store'
    path = File.join(dir, entry)
    
    if File.directory?(path)
      # Create subgroup with path relative to the parent group
      subgroup = group.new_group(entry, entry)
      add_files_recursively(path, subgroup, target, project)
    elsif entry.end_with?('.swift')
      # Add file reference
      file_ref = group.new_file(entry)
      # Add to build phases
      target.add_file_references([file_ref])
    end
  end
end

# Create iOS App target
app_target = project.new_target(:application, 'CelestiaTrace', :ios, '17.0')
app_target.build_configurations.each do |config|
  config.build_settings['SWIFT_VERSION'] = '5.9'
  config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '17.0'
  config.build_settings['TARGETED_DEVICE_FAMILY'] = '1' # 1 for iPhone, 2 for iPad, 1,2 for both
  config.build_settings['SDKROOT'] = 'iphoneos'
  config.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = 'com.celestia.trace'
  config.build_settings['GENERATE_INFOPLIST_FILE'] = 'NO'
  config.build_settings['INFOPLIST_FILE'] = 'CelestiaTrace/Resources/Info.plist'
  config.build_settings['INFOPLIST_KEY_UILaunchStoryboardName'] = 'LaunchScreen'
  config.build_settings['INFOPLIST_KEY_CFBundleDisplayName'] = '星痕'
  config.build_settings['INFOPLIST_KEY_UISupportedInterfaceOrientations'] = 'UIInterfaceOrientationPortrait'
  config.build_settings['INFOPLIST_KEY_NSMicrophoneUsageDescription'] = '需要麦克风权限以进行会议现场的音轨录音'
  config.build_settings['INFOPLIST_KEY_NSCameraUsageDescription'] = '需要相机权限以在现场录制中拍摄照片记录'
  config.build_settings['INFOPLIST_KEY_NSPhotoLibraryUsageDescription'] = '需要相册权限以选择照片记录'
  config.build_settings['INFOPLIST_KEY_NSBluetoothAlwaysUsageDescription'] = '需要蓝牙权限以扫描和绑定 BLE 录音设备'
  config.build_settings['INFOPLIST_KEY_NSBluetoothPeripheralUsageDescription'] = '需要蓝牙权限以与录音设备传输数据'
  config.build_settings['MARKETING_VERSION'] = '1.0.0'
  config.build_settings['CURRENT_PROJECT_VERSION'] = '1'
  config.build_settings['ASSETCATALOG_COMPILER_APPICON_NAME'] = 'AppIcon'
  config.build_settings['CODE_SIGN_STYLE'] = 'Automatic'
  # Enable SwiftData & SwiftUI previews support
  config.build_settings['DEVELOPMENT_ASSET_PATHS'] = '""'
end

# Create iOS Test target
test_target = project.new_target(:unit_test_bundle, 'CelestiaTraceTests', :ios, '17.0')
test_target.build_configurations.each do |config|
  config.build_settings['SWIFT_VERSION'] = '5.9'
  config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '17.0'
  config.build_settings['SDKROOT'] = 'iphoneos'
  config.build_settings['GENERATE_INFOPLIST_FILE'] = 'YES'
  config.build_settings['TEST_TARGET_NAME'] = 'CelestiaTrace'
  config.build_settings['BUNDLE_LOADER'] = '$(TEST_HOST)'
  config.build_settings['TEST_HOST'] = '$(BUILT_PRODUCTS_DIR)/CelestiaTrace.app/CelestiaTrace'
end

# Set up target dependency: Test target depends on App target
test_target.add_dependency(app_target)

# Add files to targets
add_files_recursively('CelestiaTrace', app_group, app_target, project)
add_files_recursively('Tests/CelestiaTraceTests', tests_group, test_target, project)

# Add the asset catalog (AppIcon and launch-screen color) to the app target.
assets_ref = app_group.new_file('Resources/Assets.xcassets')
app_target.resources_build_phase.add_file_reference(assets_ref)
launch_screen_ref = app_group.new_file('Resources/LaunchScreen.storyboard')
app_target.resources_build_phase.add_file_reference(launch_screen_ref)
app_group.new_file('Resources/Info.plist')

# Save project
project.save
puts "Successfully created CelestiaTrace.xcodeproj"
