//
// SplitListView.swift
// Workouts
//
// Created by rzen on 7/25/25 at 6:24 PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//

import SwiftUI
import SwiftData

struct SplitListView: View {
    @Environment(SyncEngine.self) private var sync
    @Environment(\.modelContext) private var modelContext

    @Query(sort: \Split.order) private var splits: [Split]

    var body: some View {
        ScrollView {
            LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 16) {
                ForEach(splits) { split in
                    NavigationLink {
                        SplitDetailView(split: split)
                    } label: {
                        SplitItem(split: split)
                    }
                }
            }
            .padding()
        }
        .overlay {
            if splits.isEmpty {
                ContentUnavailableView(
                    label: {
                        Label("No Splits Yet", systemImage: "dumbbell.fill")
                    },
                    description: {
                        Text("Create a split to organize your workout routine.")
                    },
                    actions: {
                        Button("Add Starter Splits") {
                            Task { await SplitSeeder.seedDefaults(into: modelContext, using: sync) }
                        }
                        .buttonStyle(.borderedProminent)
                    }
                )
            }
        }
    }
}
