//
//  vibesApp.swift
//  vibes
//
//  Created by Yamill Vallecillo on 6/30/25.
//

import SwiftUI

@main
struct vibesApp: App {
    init() {
        // Clear UserDefaults for prototype testing
        clearPrototypeData()
        
        // Force dark mode for the entire app
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
            windowScene.windows.forEach { window in
                window.overrideUserInterfaceStyle = .dark
            }
        }

        // Set dark mode as default for all new windows
        UIWindow.appearance().overrideUserInterfaceStyle = .dark
        
        // Register custom fonts
        registerFonts()
    }
    
    var body: some Scene {
        WindowGroup {
            IndexView()
                .preferredColorScheme(.dark)
                .onAppear {
                    // Ensure dark mode is applied when the view appears
                    if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
                        windowScene.windows.forEach { window in
                            window.overrideUserInterfaceStyle = .dark
                        }
                    }
                }
        }
    }
    
    private func clearPrototypeData() {
        let userDefaults = UserDefaults.standard
        
        // Clear workspace-related data
        userDefaults.removeObject(forKey: "SavedWorkspaces")
        userDefaults.removeObject(forKey: "WorkspaceSongs")
        
        // Clear any other prototype data keys here as needed
        // userDefaults.removeObject(forKey: "OtherPrototypeKey")
        
        // Force synchronization
        userDefaults.synchronize()
        
        print("🧹 Cleared all prototype data from UserDefaults")
    }
    
    private func registerFonts() {
        // Register fonts from the fonts directory
        let fontFiles = [
            "InputSans-Regular.ttf",
            "InputSans-Light.ttf", 
            "InputSans-Medium.ttf",
            "InputSans-ExtraLight.ttf",
            "PPNeueMontreal-Regular.otf",
            "PPNeueMontreal-Medium.otf",
            "PPNeueMontreal-SemiBold.otf",
            "PPNeueMontreal-Book.otf",
            "PPNeueMontreal-Italic.otf",
            "PPNeueMontreal-MediumItalic.otf",
            "PPNeueMontreal-BookItalic.otf",
            "PPNeueMontreal-SemiBolditalic.otf",
            "PPEditorialNew-Regular.otf",
            "PPEditorialNew-Light.otf",
            "PPEditorialNew-Italic.otf",
            "PPEditorialNew-LightItalic.otf"
        ]
        
        // Try to register fonts with different approaches
        for fontFile in fontFiles {
            let components = fontFile.components(separatedBy: ".")
            let fileName = components.first!
            let fileExtension = components.last!
            
            var fontURL: URL?
            
            // Try different ways to find the font
            // 1. In fonts subdirectory
            fontURL = Bundle.main.url(forResource: fileName, withExtension: fileExtension, subdirectory: "fonts")
            
            // 2. In main bundle
            if fontURL == nil {
                fontURL = Bundle.main.url(forResource: fileName, withExtension: fileExtension)
            }
            
            // 3. Try with full path
            if fontURL == nil, let bundlePath = Bundle.main.resourcePath {
                let fullPath = "\(bundlePath)/fonts/\(fontFile)"
                if FileManager.default.fileExists(atPath: fullPath) {
                    fontURL = URL(fileURLWithPath: fullPath)
                }
            }
            
            if let url = fontURL {
                var error: Unmanaged<CFError>?
                CTFontManagerRegisterFontsForURL(url as CFURL, .process, &error)
            }
        }
    }
}
