//
//  UnitSystem.swift
//  Cable
//
//  Created by Stefan Lange-Hegermann on 11.09.25.
//

import Foundation

enum UnitSystem: String, CaseIterable {
    case metric = "metric"
    case imperial = "imperial"
    
    var displayName: String {
        switch self {
        case .metric:
            return String(localized: "units.metric.display", comment: "Display name for the metric unit system")
        case .imperial:
            return String(localized: "units.imperial.display", comment: "Display name for the imperial unit system")
        }
    }
    
    var wireAreaUnit: String {
        switch self {
        case .metric:
            return "mm²"
        case .imperial:
            return "AWG"
        }
    }
    
    var lengthUnit: String {
        switch self {
        case .metric:
            return "m"
        case .imperial:
            return "ft"
        }
    }
}

@MainActor
class UnitSystemSettings: ObservableObject {
    @Published var unitSystem: UnitSystem {
        didSet {
            UserDefaults.standard.set(unitSystem.rawValue, forKey: "unitSystem")
        }
    }
    
    init() {
        let savedSystem = UserDefaults.standard.string(forKey: "unitSystem") ?? UnitSystem.metric.rawValue
        self.unitSystem = UnitSystem(rawValue: savedSystem) ?? .metric
    }
}
