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

import Foundation
import SwiftData

class CableCalculator: ObservableObject {
    @Published var voltage: Double = 12.0
    @Published var current: Double = 5.0
    @Published var power: Double = 60.0
    @Published var length: Double = 10.0
    @Published var loadName: String = String(localized: "default.load.name", comment: "Default placeholder name for a load")
    @Published var dutyCyclePercent: Double = 100.0
    @Published var dailyUsageHours: Double = 24.0
    
    var calculatedPower: Double {
        voltage * current
    }
    
    var calculatedCurrent: Double {
        voltage > 0 ? power / voltage : 0
    }
    
    func updateFromCurrent() {
        power = voltage * current
    }
    
    func updateFromPower() {
        current = voltage > 0 ? power / voltage : 0
    }
    
    func recommendedCrossSection(for unitSystem: UnitSystem) -> Double {
        ElectricalCalculations.recommendedCrossSection(
            length: length,
            current: current,
            voltage: voltage,
            unitSystem: unitSystem
        )
    }
    
    func crossSection(for unitSystem: UnitSystem) -> Double {
        recommendedCrossSection(for: unitSystem)
    }
    
    func voltageDrop(for unitSystem: UnitSystem) -> Double {
        ElectricalCalculations.voltageDrop(
            length: length,
            current: current,
            voltage: voltage,
            unitSystem: unitSystem
        )
    }
    
    func voltageDropPercentage(for unitSystem: UnitSystem) -> Double {
        ElectricalCalculations.voltageDropPercentage(
            length: length,
            current: current,
            voltage: voltage,
            unitSystem: unitSystem
        )
    }
    
    func powerLoss(for unitSystem: UnitSystem) -> Double {
        ElectricalCalculations.powerLoss(
            length: length,
            current: current,
            voltage: voltage,
            unitSystem: unitSystem
        )
    }
    
    var recommendedFuse: Double {
        ElectricalCalculations.recommendedFuse(forCurrent: current)
    }

    var recommendedFuseFormatted: String {
        let fuse = recommendedFuse
        return fuse == fuse.rounded() ? String(format: "%.0f", fuse) : String(format: "%.1f", fuse)
    }
}

@Model
class ElectricalSystem {
    var name: String = ""
    var location: String = ""
    var timestamp: Date = Date()
    var iconName: String = "building.2"
    var colorName: String = "blue"
    var targetRuntimeHours: Double?
    var targetChargeTimeHours: Double?
    
    init(
        name: String,
        location: String = "",
        iconName: String = "building.2",
        colorName: String = "blue",
        targetRuntimeHours: Double? = nil,
        targetChargeTimeHours: Double? = nil
    ) {
        self.name = name
        self.location = location
        self.timestamp = Date()
        self.iconName = iconName
        self.colorName = colorName
        self.targetRuntimeHours = targetRuntimeHours
        self.targetChargeTimeHours = targetChargeTimeHours
    }
}

@Model
class SavedLoad {
    var name: String = ""
    var voltage: Double = 0.0
    var current: Double = 0.0
    var power: Double = 0.0
    var length: Double = 0.0
    var crossSection: Double = 0.0
    var timestamp: Date = Date()
    var iconName: String = "lightbulb"
    var colorName: String = "blue"
    var isWattMode: Bool = false
    var dutyCyclePercent: Double = 100.0
    var dailyUsageHours: Double = 24.0
    var system: ElectricalSystem?
    var remoteIconURLString: String? = nil
    var componentID: String? = nil
    var bomCompletedItemIDs: [String] = []
    var identifier: String = UUID().uuidString
    
    init(
        name: String,
        voltage: Double,
        current: Double,
        power: Double,
        length: Double,
        crossSection: Double,
        iconName: String = "lightbulb",
        colorName: String = "blue",
        isWattMode: Bool = false,
        dutyCyclePercent: Double = 100.0,
        dailyUsageHours: Double = 24.0,
        system: ElectricalSystem? = nil,
        remoteIconURLString: String? = nil,
        componentID: String? = nil,
        bomCompletedItemIDs: [String] = [],
        identifier: String = UUID().uuidString
    ) {
        self.name = name
        self.voltage = voltage
        self.current = current
        self.power = power
        self.length = length
        self.crossSection = crossSection
        self.timestamp = Date()
        self.iconName = iconName
        self.colorName = colorName
        self.isWattMode = isWattMode
        self.dutyCyclePercent = dutyCyclePercent
        self.dailyUsageHours = dailyUsageHours
        self.system = system
        self.remoteIconURLString = remoteIconURLString
        self.componentID = componentID
        self.bomCompletedItemIDs = bomCompletedItemIDs
        self.identifier = identifier
    }
}
