import SwiftUI

struct TermsAgreementCard: View {
    @Binding var isAccepted: Bool
    var openTerms: () -> Void

    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            HStack(alignment: .top, spacing: 12) {
                Button {
                    isAccepted.toggle()
                } label: {
                    Image(systemName: isAccepted ? "checkmark.square.fill" : "square")
                        .font(.system(size: 24, weight: .semibold))
                        .foregroundColor(isAccepted ? .blue : .secondary)
                }
                .buttonStyle(.plain)
                .accessibilityLabel(NSLocalizedString("Согласиться с правилами", comment: ""))
                .accessibilityValue(isAccepted ? NSLocalizedString("Включено", comment: "") : NSLocalizedString("Выключено", comment: ""))

                VStack(alignment: .leading, spacing: 6) {
                    Text(NSLocalizedString("Я ознакомился и принимаю правила сервиса", comment: ""))
                        .font(.subheadline)
                        .foregroundColor(.primary)

                    Button(action: openTerms) {
                        HStack(spacing: 4) {
                            Text(NSLocalizedString("Открыть правила", comment: ""))
                            Image(systemName: "arrow.up.right")
                                .font(.caption)
                        }
                    }
                    .buttonStyle(.plain)
                    .font(.footnote)
                    .foregroundColor(.blue)
                }
            }
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding(16)
        .background(Color(.secondarySystemBackground))
        .cornerRadius(14)
    }
}

struct TermsFullScreenView: View {
    @Binding var isPresented: Bool
    var title: String
    var content: String
    var isLoading: Bool
    var errorMessage: String?
    var onRetry: () -> Void

    var body: some View {
        NavigationView {
            Group {
                if isLoading {
                    ProgressView()
                } else if let errorMessage {
                    VStack(spacing: 16) {
                        Text(errorMessage)
                            .multilineTextAlignment(.center)
                            .padding(.horizontal)
                        Button(action: onRetry) {
                            Text(NSLocalizedString("Повторить", comment: ""))
                                .padding(.horizontal, 24)
                                .padding(.vertical, 12)
                                .background(Color.blue)
                                .foregroundColor(.white)
                                .cornerRadius(12)
                        }
                    }
                } else {
                    ScrollView {
                        VStack(alignment: .leading, spacing: 16) {
                            if let attributed = try? AttributedString(markdown: content) {
                                Text(attributed)
                            } else {
                                Text(content)
                            }
                        }
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding()
                    }
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Color(.systemBackground))
            .navigationTitle(title)
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button(action: { isPresented = false }) {
                        Text(NSLocalizedString("Закрыть", comment: ""))
                    }
                }
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

