//
//  DataControllerUPDATE.swift
//  Reflect
//
//  SwiftData UPDATE operations.
//

import SwiftData
import Foundation

extension DataController {
    @discardableResult
    func update(entryDate: Date, withMood mood: Mood) -> Bool {
        guard let entry = getEntry(byDate: entryDate) else {
            return false
        }

        entry.moodValue = mood.rawValue
        entry.timestamp = Date()
        saveAndRunDataListeners()

        AnalyticsManager.shared.track(.moodUpdated(mood: mood.rawValue))
        return true
    }

    // MARK: - Notes

    @discardableResult
    func updateNotes(forDate date: Date, notes: String?) -> Bool {
        guard let entry = getEntry(byDate: date) else {
            return false
        }

        entry.notes = notes
        saveAndRunDataListeners()

        AnalyticsManager.shared.track(.noteUpdated(characterCount: (notes ?? "").count))
        return true
    }

    // MARK: - Photo

    @discardableResult
    func updatePhoto(forDate date: Date, photoID: UUID?) -> Bool {
        guard let entry = getEntry(byDate: date) else {
            return false
        }

        entry.photoID = photoID
        saveAndRunDataListeners()

        if photoID != nil {
            AnalyticsManager.shared.track(.photoAdded)
        } else {
            AnalyticsManager.shared.track(.photoDeleted)
        }
        return true
    }
}
