#!/usr/bin/env ruby
require 'xcodeproj'

# Open the project
project_path = 'lichunWebsocket.xcodeproj'
project = Xcodeproj::Project.open(project_path)

# Get the main target
target = project.targets.first

# Remove the incorrectly added files
target.source_build_phase.files.to_a.reverse_each do |build_file|
  path = build_file.file_ref.path
  if path && (path.include?('lichunWebsocket/Shared/Components/lichunWebsocket') || path.include?('lichunWebsocket/Features/lichunWebsocket'))
    puts "Removing incorrectly added file: #{path}"
    build_file.remove_from_project
  end
end

# Find the correct groups
main_group = project.main_group['lichunWebsocket']
shared_group = main_group['Shared']
components_group = shared_group['Components']
features_group = main_group['Features']

# Create Navigation group if it doesn't exist
navigation_group = components_group['Navigation'] || components_group.new_group('Navigation')

# Add Navigation files with correct paths
navigation_files = [
  '../lichunWebsocket/Shared/Components/Navigation/LiquidGlassTabBar.swift',
  '../lichunWebsocket/Shared/Components/Navigation/LiquidGlassTabContainer.swift',
  '../lichunWebsocket/Shared/Components/Navigation/QuickActionsMenu.swift'
]

navigation_files.each do |file|
  file_ref = navigation_group.new_reference(file)
  target.add_file_references([file_ref])
end

# Create Social group if it doesn't exist
social_group = features_group['Social'] || features_group.new_group('Social')

# Add Social file with correct path
social_file = '../lichunWebsocket/Features/Social/SocialView.swift'
social_file_ref = social_group.new_reference(social_file)
target.add_file_references([social_file_ref])

# Save the project
project.save

puts "Files fixed successfully!"
