import SwiftUI
import PoseCore

struct CoachingChecklistView: View {
    @ObservedObject var model: ReviewerModel
    var body: some View {
        let review = model.currentCoachingChecklist
        let rows = CoachingChecklist.rows(review: review)
        let assessed = rows.filter { $0.status != .needsReview }.count
        VStack(alignment: .leading, spacing: 8) {
            Text("Whole-body review · \(assessed) of \(rows.count) topics assessed").font(.subheadline).fontWeight(.medium)
            if let error = model.coachingChecklistError { Text(error).font(.caption).foregroundStyle(.orange) }
            if model.checklistDirectionConflict {
                Text("Saved findings use a different turn direction. Restore the photo direction or reassess this checklist.").font(.caption).foregroundStyle(.orange)
            }
            ForEach(rows.filter { $0.status == .focus || $0.status == .hypothesis }) { item in
                VStack(alignment: .leading, spacing: 3) {
                    Text("\(item.topic.title) · \(item.status.label)").fontWeight(.medium)
                    Text(item.observation)
                }.font(.caption).textSelection(.enabled)
            }
            DisclosureGroup("All topics and practice cues") {
                ForEach(rows) { item in
                    VStack(alignment: .leading, spacing: 4) {
                        Text("\(item.topic.title) · \(item.status.label)").fontWeight(.medium)
                        if !item.observation.isEmpty { Text(item.observation) }
                        Text("Review cue: " + item.topic.cue).foregroundStyle(.secondary)
                    }.font(.caption).frame(maxWidth: .infinity, alignment: .leading).padding(.vertical, 5).textSelection(.enabled)
                }
                Text(review?.provenance ?? "Checklist added automatically. Photo-specific findings await visual review; no fault has been detected.")
                    .font(.caption2).foregroundStyle(.secondary).textSelection(.enabled)
            }
        }.frame(maxWidth: .infinity, alignment: .leading)
    }
}
