import Foundation
import SwiftData

@Model
class SavedBattery {
    @Attribute(.unique) var id: UUID
    var name: String
    var nominalVoltage: Double
    var capacityAmpHours: Double
    private var chemistryRawValue: String
    var system: ElectricalSystem?
    var timestamp: Date

    init(
        id: UUID = UUID(),
        name: String,
        nominalVoltage: Double = 12.8,
        capacityAmpHours: Double = 100,
        chemistry: BatteryConfiguration.Chemistry = .lithiumIronPhosphate,
        system: ElectricalSystem? = nil,
        timestamp: Date = Date()
    ) {
        self.id = id
        self.name = name
        self.nominalVoltage = nominalVoltage
        self.capacityAmpHours = capacityAmpHours
        self.chemistryRawValue = chemistry.rawValue
        self.system = system
        self.timestamp = timestamp
    }

    var chemistry: BatteryConfiguration.Chemistry {
        get {
            BatteryConfiguration.Chemistry(rawValue: chemistryRawValue) ?? .lithiumIronPhosphate
        }
        set {
            chemistryRawValue = newValue.rawValue
        }
    }

    var energyWattHours: Double {
        nominalVoltage * capacityAmpHours
    }
}
