import Foundation
import SharedModels
import SwiftData

struct CourseReviewStore {
    let context: ModelContext

    @discardableResult
    func fetchOrCreateReviewCard(for card: VocabCard) -> CourseReviewCard {
        let reviewKey = CourseCardStore.reviewKey(for: card)
        let descriptor = FetchDescriptor<CourseReviewCard>(
            predicate: #Predicate<CourseReviewCard> { $0.id == reviewKey }
        )

        if let existing = (try? context.fetch(descriptor))?.first {
            return existing
        }

        let reviewCard = CourseReviewCard(
            id: reviewKey,
            deckId: card.deckId,
            front: card.front,
            back: card.back
        )
        context.insert(reviewCard)
        return reviewCard
    }

    func rate(card: VocabCard, quality: ReviewQuality) {
        let reviewCard = fetchOrCreateReviewCard(for: card)
        let result = SRSEngine.review(
            quality: quality,
            currentEase: reviewCard.easeFactor,
            currentInterval: reviewCard.interval,
            currentReps: reviewCard.repetitions
        )
        reviewCard.easeFactor = result.easeFactor
        reviewCard.interval = result.interval
        reviewCard.repetitions = result.repetitions
        reviewCard.dueDate = SRSEngine.nextDueDate(interval: result.interval)
        reviewCard.lastReviewDate = Date()
        try? context.save()
    }
}
