//
// GoalsListView.swift
// Workouts
//
// Copyright 2026 Rouslan Zenetl. All Rights Reserved.
//

import SwiftUI

/// A reorderable list of the practice-goal kinds. The chosen order is persisted via
/// `GoalKind.saveOrder` and drives how schedules group on the Today screen. Pushed
/// from Settings → Library, so it relies on Settings' own NavigationStack.
struct GoalsListView: View {
    @State private var order: [GoalKind] = []

    var body: some View {
        List {
            Section {
                ForEach(order) { kind in
                    Label(kind.displayName, systemImage: kind.systemImage)
                        .foregroundStyle(Color.color(from: kind.colorName))
                }
                .onMove(perform: move)
            } footer: {
                Text("Goals group your schedules on the Today screen. Drag to reorder.")
            }
        }
        .navigationTitle("Goals")
        .toolbar {
            EditButton()
        }
        .onAppear {
            if order.isEmpty { order = GoalKind.orderedCases() }
        }
    }

    private func move(from source: IndexSet, to destination: Int) {
        order.move(fromOffsets: source, toOffset: destination)
        GoalKind.saveOrder(order)
    }
}
