import SwiftUI
import Bedrock
import SwiftData

struct DefaultCardSelectionView: View {
    @Environment(AppState.self) private var appState

    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: Design.Spacing.small) {
                SettingsSectionHeader(
                    title: "Default Card",
                    systemImage: "checklist",
                    accentColor: AppThemeAccent.primary
                )

                SettingsCard(
                    backgroundColor: Color.AppBackground.elevated,
                    borderColor: AppBorder.standard
                ) {
                    if appState.cardStore.cards.isEmpty {
                        SettingsCardRow {
                            Text("Create a card first")
                                .typography(.body)
                                .foregroundStyle(Color.AppText.secondary)
                        }
                    } else {
                        ForEach(appState.cardStore.cards) { card in
                            Button {
                                appState.cardStore.setDefaultCard(card)
                            } label: {
                                SettingsCardRow(verticalPadding: Design.Spacing.medium) {
                                    HStack {
                                        VStack(alignment: .leading, spacing: Design.Spacing.xxxSmall) {
                                            Text(displayTitle(for: card))
                                                .typography(.bodyEmphasis)
                                                .foregroundStyle(Color.AppText.primary)

                                            if !card.label.isEmpty {
                                                Text(card.label)
                                                    .typography(.caption)
                                                    .foregroundStyle(Color.AppText.secondary)
                                            }
                                        }

                                        Spacer()

                                        if card.isDefault {
                                            Image(systemName: "checkmark")
                                                .foregroundStyle(AppThemeAccent.primary)
                                                .font(.body.bold())
                                        }
                                    }
                                }
                                .background(Color.clear)
                            }
                            .buttonStyle(.plain)

                            if card.id != appState.cardStore.cards.last?.id {
                                SettingsDivider(color: AppBorder.subtle)
                            }
                        }
                    }
                }
            }
            .padding(.horizontal, Design.Spacing.large)
            .padding(.top, Design.Spacing.medium)
            .padding(.bottom, Design.Spacing.xxxLarge)
        }
        .background(Color.AppBackground.base)
        .navigationTitle("Default Card")
        .navigationBarTitleDisplayMode(.inline)
    }

    private func displayTitle(for card: BusinessCard) -> String {
        let trimmedName = card.fullName.trimmingCharacters(in: .whitespacesAndNewlines)
        if !trimmedName.isEmpty {
            return trimmedName
        }
        let trimmedLabel = card.label.trimmingCharacters(in: .whitespacesAndNewlines)
        if !trimmedLabel.isEmpty {
            return trimmedLabel
        }
        return "Untitled"
    }
}

#Preview {
    DefaultCardSelectionView()
        .environment(AppState(modelContext: try! ModelContainer(for: BusinessCard.self, Contact.self, AppSettings.self).mainContext))
}
