//
//  SQPasswordField.swift
//  Allovoisins
//
//  Created by Claude on 22/10/2025.
//  Copyright © 2025 AlloVoisins. All rights reserved.
//

import SwiftUI

// MARK: - SQPasswordField

/// Specialized text field for password input with visibility toggle.
///
/// Use View Modifiers to configure the password field:
/// ```swift
/// SQPasswordField("Mot de passe", text: $password)
///     .sqPlaceholder("Entrez votre mot de passe")
///     .sqMinCharacters(8)
///     .sqHelperText("Minimum 8 caractères")
///     .sqError($passwordError)
/// ```
struct SQPasswordField: View {
    private let label: String
    @Binding var text: String

    @FocusState private var isFocused: Bool
    @State private var isPasswordVisible = false
    @Environment(\.sqTextFieldConfiguration) private var config

    private var accentColor: Color { .sqNeutral(80) }

    /// Creates a password field with visibility toggle.
    ///
    /// - Parameters:
    ///   - label: The label text displayed above the field
    ///   - text: Binding to the password text value
    init(_ label: String, text: Binding<String>) {
        self.label = label
        self._text = text
    }

    var body: some View {
        SQTextFieldBase(label, isFocused: Binding(get: { isFocused }, set: { isFocused = $0 }), textCount: text.count) {
            HStack(spacing: 4) {
                Group {
                    if isPasswordVisible {
                        TextField("", text: $text)
                    } else {
                        SecureField("", text: $text)
                    }
                }
                .placeholder(when: text.isEmpty) {
                    SQText(config.placeholder)
                        .sqColor(.sqNeutral(50))
                }
                .onChange(of: text) { newValue in
                    // Character limit
                    if let max = config.maxCharacters, newValue.count > max {
                        text = String(newValue.prefix(max))
                    }
                    // Reset error
                    config.error.wrappedValue = .none
                }
                .keyboardType(config.keyboardType)
                .foregroundStyle(Color.sqNeutral(100))
                .tint(accentColor)
                .submitLabel(.done)
                .focused($isFocused)
                .accessibilityLabel(label)
                .accessibilityValue(text.isEmpty ? "Vide" : "Rempli")

                Button {
                    isPasswordVisible.toggle()
                } label: {
                    SQIcon(isPasswordVisible ? .eye_slash : .eye, color: .sqNeutral(80))
                }
                .accessibilityLabel(isPasswordVisible ? "Masquer le mot de passe" : "Afficher le mot de passe")
            }
        }
    }
}

// MARK: - Preview

#Preview {
    VStack(spacing: 32) {
        SQPasswordField("Nouveau mot de passe", text: .constant(""))
            .sqPlaceholder("Mot de passe")

        SQPasswordField("Mot de passe", text: .constant("MonMotDePasse123!"))
            .sqPlaceholder("Mot de passe")

        SQPasswordField("Mot de passe", text: .constant(""))
            .sqPlaceholder("Mot de passe")
            .sqOptional()

        SQPasswordField("Mot de passe", text: .constant(""))
            .sqPlaceholder("Mot de passe")
            .disabled(true)

        SQPasswordField("Nouveau mot de passe", text: .constant(""))
            .sqPlaceholder("Mot de passe")
            .sqMinCharacters(8)
            .sqHelperText("Minimum 8 caractères")

        SQPasswordField("Confirmer le mot de passe", text: .constant(""))
            .sqPlaceholder("Mot de passe")
            .sqTooltipText("Le mot de passe doit contenir au moins 8 caractères, une majuscule, une minuscule et un chiffre.")
    }
    .padding()
}
