import Combine
import Foundation

@MainActor
final class StudyHomeViewModel: ObservableObject {
    @Published var tasks: [ZXSTask] = [
        ZXSTask(t: "机器学习 - 回忆测试", tp: "回忆测试", ch: "#7C6EFA", m: 10, d: true),
        ZXSTask(t: "高数 - 间隔复习 8 题", tp: "间隔复习", ch: "#F97316", m: 15, d: true),
        ZXSTask(t: "英语词汇 - 25 个待复习", tp: "词汇复习", ch: "#2DD4BF", m: 8, d: false),
        ZXSTask(t: "注意力机制 - 费曼解释", tp: "费曼练习", ch: "#A78BFA", m: 12, d: false),
        ZXSTask(t: "产品设计 - 薄弱点复习", tp: "薄弱点", ch: "#F59E0B", m: 10, d: false),
    ]

    @Published var weekActivity: [CGFloat] = [0.3, 0.7, 1.0, 0.4, 0.9, 0.6, 0.2]
    let dayLabels = ["一", "二", "三", "四", "五", "六", "日"]

    var doneCount: Int { tasks.filter(\.d).count }
    var progress: Double { tasks.isEmpty ? 0 : Double(doneCount) / Double(tasks.count) }
    var doneMinutes: Int { tasks.filter(\.d).map(\.m).reduce(0, +) }
    var remainingMinutes: Int { tasks.filter { !$0.d }.map(\.m).reduce(0, +) }

    func toggleTask(_ task: ZXSTask) {
        guard let idx = tasks.firstIndex(where: { $0.id == task.id }) else { return }
        tasks[idx].d.toggle()
    }
}
