import Foundation
import SwiftData

/// A habit within a specific ritual arc. Each arc has its own copy of habits
/// with their own completion tracking, allowing habits to change between arcs
/// while preserving historical data.
@Model
final class ArcHabit {
    var id: UUID = UUID()
    var title: String = ""
    var symbolName: String = ""
    var goal: String = ""
    var completedDayIDs: [String] = []
    
    /// Persisted sort order for deterministic ordering across CloudKit sync.
    var sortIndex: Int = 0
    
    @Relationship(inverse: \RitualArc.habits)
    var arc: RitualArc?

    init(
        id: UUID = UUID(),
        title: String,
        symbolName: String,
        goal: String = "",
        sortIndex: Int = 0,
        completedDayIDs: [String] = []
    ) {
        self.id = id
        self.title = title
        self.symbolName = symbolName
        self.goal = goal
        self.sortIndex = sortIndex
        self.completedDayIDs = completedDayIDs
    }
    
    /// Creates a copy of this habit for a new arc (without completions).
    func copyForNewArc() -> ArcHabit {
        ArcHabit(
            title: title,
            symbolName: symbolName,
            goal: goal,
            sortIndex: sortIndex,
            completedDayIDs: []
        )
    }
}
