package app.voltplan.cable.data.model

import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import java.util.UUID

/**
 * Room entities mirroring the iOS SwiftData models. Parent/child links use a nullable
 * [systemId] string instead of an object relationship; all values are stored in metric.
 */

@Entity(tableName = "systems")
data class ElectricalSystem(
    @PrimaryKey val id: String = UUID.randomUUID().toString(),
    val name: String = "",
    val location: String = "",
    val timestamp: Long = System.currentTimeMillis(),
    val iconName: String = "building.2",
    val colorName: String = "blue",
    val targetRuntimeHours: Double? = null,
    val targetChargeTimeHours: Double? = null,
)

@Entity(
    tableName = "loads",
    indices = [Index("systemId")],
)
data class SavedLoad(
    @PrimaryKey val id: String = UUID.randomUUID().toString(),
    val name: String = "",
    val voltage: Double = 0.0,
    val current: Double = 0.0,
    val power: Double = 0.0,
    val length: Double = 0.0,
    val crossSection: Double = 0.0,
    val timestamp: Long = System.currentTimeMillis(),
    val iconName: String = "lightbulb",
    val colorName: String = "blue",
    val isWattMode: Boolean = false,
    val dutyCyclePercent: Double = 100.0,
    val dailyUsageHours: Double = 24.0,
    val systemId: String? = null,
    val remoteIconURLString: String? = null,
    val affiliateURLString: String? = null,
    val affiliateCountryCode: String? = null,
    val bomCompletedItemIDs: List<String> = emptyList(),
)

@Entity(
    tableName = "batteries",
    indices = [Index("systemId")],
)
data class SavedBattery(
    @PrimaryKey val id: String = UUID.randomUUID().toString(),
    val name: String = "",
    val nominalVoltage: Double = 12.8,
    val capacityAmpHours: Double = 100.0,
    val usableCapacityOverrideFraction: Double? = null,
    val chargeVoltage: Double? = null,
    val cutOffVoltage: Double? = null,
    val minimumTemperatureCelsius: Double? = null,
    val maximumTemperatureCelsius: Double? = null,
    val chemistryRawValue: String = Chemistry.LIFEPO4.rawValue,
    val iconName: String = "battery.100",
    val colorName: String = "blue",
    val systemId: String? = null,
    val affiliateURLString: String? = null,
    val affiliateCountryCode: String? = null,
    val bomCompletedItemIDs: List<String> = emptyList(),
    val timestamp: Long = System.currentTimeMillis(),
)

@Entity(
    tableName = "chargers",
    indices = [Index("systemId")],
)
data class SavedCharger(
    @PrimaryKey val id: String = UUID.randomUUID().toString(),
    val name: String = "",
    val inputVoltage: Double = 230.0,
    val outputVoltage: Double = 14.2,
    val maxCurrentAmps: Double = 30.0,
    val maxPowerWatts: Double = 0.0,
    val iconName: String = "bolt.fill",
    val colorName: String = "orange",
    val systemId: String? = null,
    val timestamp: Long = System.currentTimeMillis(),
    val remoteIconURLString: String? = null,
    val affiliateURLString: String? = null,
    val affiliateCountryCode: String? = null,
    val bomCompletedItemIDs: List<String> = emptyList(),
    val powerSourceType: String = PowerSourceType.SHORE.rawValue,
)

// --- Derived metrics (mirroring the iOS computed properties) ---

val SavedBattery.chemistry: Chemistry get() = Chemistry.fromRaw(chemistryRawValue)
val SavedBattery.energyWattHours: Double get() = nominalVoltage * capacityAmpHours
val SavedBattery.usableCapacityFraction: Double
    get() = (usableCapacityOverrideFraction ?: chemistry.usableCapacityFraction).coerceIn(0.0, 1.0)
val SavedBattery.usableCapacityAmpHours: Double get() = capacityAmpHours * usableCapacityFraction
val SavedBattery.usableEnergyWattHours: Double get() = usableCapacityAmpHours * nominalVoltage

val SavedCharger.sourceType: PowerSourceType get() = PowerSourceType.fromRaw(powerSourceType)
val SavedCharger.effectivePowerWatts: Double
    get() = if (maxPowerWatts > 0) maxPowerWatts else outputVoltage * maxCurrentAmps
