import SwiftUI

// MARK: - Password Strength

enum PasswordStrength: Int {
    case weak = 0
    case medium = 1
    case strong = 2

    static let minimumLength = 6
    static let strongLength = 10

    init(password: String) {
        if password.count < Self.minimumLength {
            self = .weak
        } else if password.count < Self.strongLength {
            self = .medium
        } else {
            self = .strong
        }
    }

    var label: String {
        switch self {
        case .weak: return "Weak"
        case .medium: return "Medium"
        case .strong: return "Strong"
        }
    }

    var color: Color {
        switch self {
        case .weak: return RosettaColors.error
        case .medium: return RosettaColors.warning
        case .strong: return RosettaColors.success
        }
    }
}

// MARK: - Strength Indicator View

struct PasswordStrengthIndicator: View {
    let password: String

    private var strength: PasswordStrength {
        PasswordStrength(password: password)
    }

    var body: some View {
        if !password.isEmpty {
            VStack(alignment: .leading, spacing: 6) {
                HStack(spacing: 4) {
                    ForEach(0..<3, id: \.self) { index in
                        RoundedRectangle(cornerRadius: 2)
                            .fill(index <= strength.rawValue ? strength.color : RosettaColors.Adaptive.text.opacity(0.1))
                            .frame(height: 4)
                            .animation(.easeInOut(duration: 0.25), value: strength)
                    }
                }

                Text("Password strength: \(strength.label)")
                    .font(.system(size: 12))
                    .foregroundStyle(strength.color)
            }
            .transition(.opacity)
            .accessibilityLabel("Password strength: \(strength.label)")
        }
    }
}

// MARK: - Match Indicator View

struct PasswordMatchIndicator: View {
    let password: String
    let confirmPassword: String

    private var matches: Bool {
        password == confirmPassword
    }

    var body: some View {
        if !confirmPassword.isEmpty {
            HStack(spacing: 6) {
                Image(systemName: matches ? "checkmark.circle.fill" : "xmark.circle.fill")
                    .font(.system(size: 14))
                Text(matches ? "Passwords match" : "Passwords don't match")
                    .font(.system(size: 12))
            }
            .foregroundStyle(matches ? RosettaColors.success : RosettaColors.error)
            .transition(.opacity)
            .accessibilityLabel(matches ? "Passwords match" : "Passwords do not match")
        }
    }
}

// MARK: - Weak Password Warning

struct WeakPasswordWarning: View {
    let password: String

    var body: some View {
        if !password.isEmpty && PasswordStrength(password: password) == .weak {
            HStack(alignment: .top, spacing: 10) {
                Image(systemName: "exclamationmark.triangle.fill")
                    .foregroundStyle(RosettaColors.warning)
                    .font(.system(size: 16))

                Text("Your password is too weak. Consider using at least 6 characters for better security.")
                    .font(.system(size: 13))
                    .foregroundStyle(RosettaColors.Adaptive.textSecondary)
                    .lineSpacing(2)
            }
            .padding(14)
            .background {
                RoundedRectangle(cornerRadius: 10)
                    .fill(RosettaColors.warning.opacity(0.1))
            }
            .transition(.opacity.combined(with: .scale(scale: 0.95)))
        }
    }
}

#Preview {
    VStack(spacing: 20) {
        PasswordStrengthIndicator(password: "abc")
        PasswordStrengthIndicator(password: "abcdefgh")
        PasswordStrengthIndicator(password: "abcdefghijk")
        PasswordMatchIndicator(password: "test", confirmPassword: "test")
        PasswordMatchIndicator(password: "test", confirmPassword: "wrong")
        WeakPasswordWarning(password: "ab")
    }
    .padding()
    .background(RosettaColors.authBackground)
}
