import Foundation
import CoreData

@objc(JournalEntry)
public class JournalEntry: NSManagedObject, Identifiable {
    @nonobjc public class func fetchRequest() -> NSFetchRequest<JournalEntry> {
        return NSFetchRequest<JournalEntry>(entityName: "JournalEntry")
    }

    @NSManaged public var id: UUID?
    @NSManaged public var monthKey: String?
    @NSManaged public var note: String?
    @NSManaged public var moodRaw: String?
    @NSManaged public var rating: Int16
    @NSManaged public var completionTime: Date?
    @NSManaged public var createdAt: Date?

    public override func awakeFromInsert() {
        super.awakeFromInsert()
        id = UUID()
        createdAt = Date()
    }

    var mood: MonthlyCheckInMood? {
        get { moodRaw.flatMap { MonthlyCheckInMood(rawValue: $0) } }
        set { moodRaw = newValue?.rawValue }
    }

    var ratingValue: Int? {
        get { rating > 0 ? Int(rating) : nil }
        set { rating = Int16(newValue.map { min(max(1, $0), 5) } ?? 0) }
    }

    var completionDate: Date? {
        get { completionTime }
        set { completionTime = newValue }
    }
}
