import SwiftUI

// MARK: - Standard Form Header Section

struct FormHeaderSection<Content: View>: View {
    let content: Content

    init(@ViewBuilder content: () -> Content) {
        self.content = content()
    }

    var body: some View {
        Section {
            content
                .frame(maxWidth: .infinity)
                .padding(.vertical)
        }
        .headerSectionBackground()
    }
}

// MARK: - Form Section with Icon

struct IconFormSection<Content: View>: View {
    let icon: String
    let title: String
    let footer: String?
    let content: Content

    init(
        icon: String,
        title: String,
        footer: String? = nil,
        @ViewBuilder content: () -> Content
    ) {
        self.icon = icon
        self.title = title
        self.footer = footer
        self.content = content()
    }

    var body: some View {
        Section {
            content
        } header: {
            HStack(spacing: 8) {
                Image(systemName: icon)
                    .font(.system(size: 14, weight: .semibold))
                    .foregroundColor(Color.appPrimary)
                Text(title)
            }
        } footer: {
            if let footer = footer {
                Text(footer)
            }
        }
        .sectionBackground()
    }
}

// MARK: - Error Display Section

struct ErrorSection: View {
    let message: String

    var body: some View {
        Section {
            HStack(spacing: 10) {
                Image(systemName: "exclamationmark.triangle.fill")
                    .foregroundColor(Color.appError)
                Text(message)
                    .foregroundColor(Color.appError)
                    .font(.subheadline)
            }
        }
        .sectionBackground()
    }
}

// MARK: - Success Display Section

struct SuccessSection: View {
    let message: String

    var body: some View {
        Section {
            HStack(spacing: 10) {
                Image(systemName: "checkmark.circle.fill")
                    .foregroundColor(Color.appPrimary)
                Text(message)
                    .foregroundColor(Color.appPrimary)
                    .font(.subheadline)
            }
        }
        .sectionBackground()
    }
}

// MARK: - Form Action Button Section

struct FormActionButton: View {
    let title: String
    let isLoading: Bool
    let action: () -> Void

    init(
        title: String,
        isLoading: Bool = false,
        action: @escaping () -> Void
    ) {
        self.title = title
        self.isLoading = isLoading
        self.action = action
    }

    var body: some View {
        Section {
            Button(action: action) {
                HStack {
                    Spacer()
                    if isLoading {
                        ProgressView()
                    } else {
                        Text(title)
                            .fontWeight(.semibold)
                    }
                    Spacer()
                }
            }
            .disabled(isLoading)
        }
        .sectionBackground()
    }
}

// MARK: - Form Header with Icon

struct FormHeader: View {
    let icon: String
    let title: String
    let subtitle: String
    let iconSize: CGFloat
    let accentColor: Color

    init(
        icon: String,
        title: String,
        subtitle: String,
        iconSize: CGFloat = 60,
        accentColor: Color = Color.appPrimary
    ) {
        self.icon = icon
        self.title = title
        self.subtitle = subtitle
        self.iconSize = iconSize
        self.accentColor = accentColor
    }

    var body: some View {
        VStack(spacing: 16) {
            Image(systemName: icon)
                .font(.system(size: iconSize))
                .foregroundStyle(accentColor.gradient)

            VStack(spacing: 8) {
                Text(title)
                    .font(.title2)
                    .fontWeight(.bold)
                    .foregroundColor(Color.appTextPrimary)

                Text(subtitle)
                    .font(.subheadline)
                    .foregroundColor(Color.appTextSecondary)
                    .multilineTextAlignment(.center)
            }
        }
    }
}

// MARK: - Organic Form Header (matches existing pattern)

struct OrganicFormHeader: View {
    let icon: String
    let title: String
    let subtitle: String
    let accentColor: Color

    init(
        icon: String,
        title: String,
        subtitle: String,
        accentColor: Color = Color.appPrimary
    ) {
        self.icon = icon
        self.title = title
        self.subtitle = subtitle
        self.accentColor = accentColor
    }

    var body: some View {
        VStack(spacing: OrganicSpacing.cozy) {
            ZStack {
                Circle()
                    .fill(
                        RadialGradient(
                            colors: [
                                accentColor.opacity(0.15),
                                accentColor.opacity(0.05),
                                Color.clear
                            ],
                            center: .center,
                            startRadius: 0,
                            endRadius: 50
                        )
                    )
                    .frame(width: 100, height: 100)

                Image(systemName: icon)
                    .font(.system(size: 56))
                    .foregroundStyle(accentColor.gradient)
            }

            Text(title)
                .font(.system(size: 22, weight: .bold, design: .rounded))
                .foregroundColor(Color.appTextPrimary)

            if !subtitle.isEmpty {
                Text(subtitle)
                    .font(.system(size: 15, weight: .medium))
                    .foregroundColor(Color.appTextSecondary)
                    .multilineTextAlignment(.center)
            }
        }
    }
}

// MARK: - Enhanced TextField with Icon

struct IconTextField: View {
    let icon: String
    let placeholder: String
    @Binding var text: String
    var isSecure: Bool = false
    var keyboardType: UIKeyboardType = .default
    var textContentType: UITextContentType? = nil
    var onSubmit: (() -> Void)? = nil

    @FocusState private var isFocused: Bool

    var body: some View {
        HStack(spacing: 12) {
            ZStack {
                Circle()
                    .fill(Color.appPrimary.opacity(0.1))
                    .frame(width: 32, height: 32)
                Image(systemName: icon)
                    .font(.system(size: 14, weight: .medium))
                    .foregroundColor(Color.appPrimary)
            }
            .accessibilityHidden(true)

            if isSecure {
                SecureField(placeholder, text: $text)
                    .font(.system(size: 16, weight: .medium))
                    .textContentType(textContentType)
                    .focused($isFocused)
                    .submitLabel(.next)
                    .onSubmit { onSubmit?() }
            } else {
                TextField(placeholder, text: $text)
                    .font(.system(size: 16, weight: .medium))
                    .keyboardType(keyboardType)
                    .textInputAutocapitalization(keyboardType == .emailAddress ? .never : .sentences)
                    .autocorrectionDisabled(keyboardType == .emailAddress)
                    .textContentType(textContentType)
                    .focused($isFocused)
                    .submitLabel(.next)
                    .onSubmit { onSubmit?() }
            }
        }
        .padding(16)
        .background(Color.appBackgroundPrimary.opacity(0.5))
        .clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
        .overlay(
            RoundedRectangle(cornerRadius: 16, style: .continuous)
                .stroke(isFocused ? Color.appPrimary : Color.appTextSecondary.opacity(0.15), lineWidth: 1.5)
        )
        .animation(.easeInOut(duration: 0.2), value: isFocused)
    }
}

// MARK: - Secure TextField with Icon and Visibility Toggle

struct SecureIconTextField: View {
    let icon: String
    let placeholder: String
    @Binding var text: String
    @Binding var isVisible: Bool
    var textContentType: UITextContentType? = nil
    var onSubmit: (() -> Void)? = nil
    var accessibilityId: String? = nil

    @FocusState private var isFocused: Bool

    var body: some View {
        HStack(spacing: 12) {
            ZStack {
                Circle()
                    .fill(Color.appPrimary.opacity(0.1))
                    .frame(width: 32, height: 32)
                Image(systemName: icon)
                    .font(.system(size: 14, weight: .medium))
                    .foregroundColor(Color.appPrimary)
            }
            .accessibilityHidden(true)

            Group {
                if isVisible {
                    TextField(placeholder, text: $text)
                        .accessibilityIdentifier(accessibilityId ?? "")
                } else {
                    SecureField(placeholder, text: $text)
                        .accessibilityIdentifier(accessibilityId ?? "")
                }
            }
            .font(.system(size: 16, weight: .medium))
            .textContentType(textContentType)
            .focused($isFocused)
            .submitLabel(.go)
            .onSubmit { onSubmit?() }

            Button(action: { isVisible.toggle() }) {
                Image(systemName: isVisible ? "eye.slash.fill" : "eye.fill")
                    .font(.system(size: 16, weight: .medium))
                    .foregroundColor(Color.appTextSecondary)
            }
            .accessibilityLabel(A11y.Auth.passwordToggle)
            .accessibilityIdentifier(AccessibilityIdentifiers.Authentication.passwordVisibilityToggle)
        }
        .padding(16)
        .background(Color.appBackgroundPrimary.opacity(0.5))
        .clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
        .overlay(
            RoundedRectangle(cornerRadius: 16, style: .continuous)
                .stroke(isFocused ? Color.appPrimary : Color.appTextSecondary.opacity(0.15), lineWidth: 1.5)
        )
        .animation(.easeInOut(duration: 0.2), value: isFocused)
    }
}

// MARK: - Field Label

struct FieldLabel: View {
    let text: String
    var isRequired: Bool = false

    var body: some View {
        HStack(spacing: 4) {
            Text(text)
                .font(.system(size: 14, weight: .medium, design: .rounded))
                .foregroundColor(Color.appTextSecondary)

            if isRequired {
                Text("*")
                    .font(.system(size: 14, weight: .bold))
                    .foregroundColor(Color.appError)
            }
        }
    }
}

// MARK: - Field Error Message

struct FieldError: View {
    let message: String

    var body: some View {
        Text(message)
            .font(.caption)
            .foregroundColor(Color.appError)
            .accessibilityLabel(message)
    }
}
