import Foundation
import SwiftData

@Model
final class SavedCharger {
    @Attribute(.unique) var id: UUID
    var name: String
    var inputVoltage: Double
    var outputVoltage: Double
    var maxCurrentAmps: Double
    var maxPowerWatts: Double
    var iconName: String
    var colorName: String
    var system: ElectricalSystem?
    var timestamp: Date
    var remoteIconURLString: String?
    var componentID: String?
    var bomCompletedItemIDs: [String] = []
    var identifier: String
    var powerSourceType: String = "shore"

    enum PowerSourceType: String, CaseIterable, Identifiable {
        case shore = "shore"
        case solar = "solar"
        case wind = "wind"
        case generator = "generator"
        case alternator = "alternator"

        var id: Self { self }

        var displayName: String {
            switch self {
            case .shore: return String(localized: "charger.source.shore", defaultValue: "Shore Power")
            case .solar: return String(localized: "charger.source.solar", defaultValue: "Solar")
            case .wind: return String(localized: "charger.source.wind", defaultValue: "Wind")
            case .generator: return String(localized: "charger.source.generator", defaultValue: "Generator")
            case .alternator: return String(localized: "charger.source.alternator", defaultValue: "Alternator")
            }
        }

        var iconName: String {
            switch self {
            case .shore: return "powerplug"
            case .solar: return "sun.max.fill"
            case .wind: return "wind"
            case .generator: return "engine.combustion.fill"
            case .alternator: return "bolt.car.fill"
            }
        }
    }

    var sourceType: PowerSourceType {
        get { PowerSourceType(rawValue: powerSourceType) ?? .shore }
        set { powerSourceType = newValue.rawValue }
    }

    init(
        id: UUID = UUID(),
        name: String,
        inputVoltage: Double = LocaleDefaults.mainsVoltage,
        outputVoltage: Double = 14.2,
        maxCurrentAmps: Double = 30.0,
        maxPowerWatts: Double = 0.0,
        iconName: String = "bolt.fill",
        colorName: String = "orange",
        system: ElectricalSystem? = nil,
        timestamp: Date = Date(),
        remoteIconURLString: String? = nil,
        componentID: String? = nil,
        bomCompletedItemIDs: [String] = [],
        identifier: String = UUID().uuidString,
        powerSourceType: String = "shore"
    ) {
        self.id = id
        self.name = name
        self.inputVoltage = inputVoltage
        self.outputVoltage = outputVoltage
        self.maxCurrentAmps = maxCurrentAmps
        self.maxPowerWatts = maxPowerWatts
        self.iconName = iconName
        self.colorName = colorName
        self.system = system
        self.timestamp = timestamp
        self.remoteIconURLString = remoteIconURLString
        self.componentID = componentID
        self.bomCompletedItemIDs = bomCompletedItemIDs
        self.identifier = identifier
        self.powerSourceType = powerSourceType
    }

    var effectivePowerWatts: Double {
        if maxPowerWatts > 0 {
            return maxPowerWatts
        }
        return outputVoltage * maxCurrentAmps
    }
}
