import Foundation
import Combine

final class ChangePasswordViewModel: ObservableObject {
    @Published private(set) var isLoading: Bool = false
    @Published var successMessage: String?
    @Published var errorMessage: String?

    private let authService: AuthService

    init(authService: AuthService = AuthService()) {
        self.authService = authService
    }

    func changePassword(oldPassword: String, newPassword: String) {
        guard !isLoading else { return }

        isLoading = true
        successMessage = nil
        errorMessage = nil

        authService.changePassword(oldPassword: oldPassword, newPassword: newPassword) { [weak self] success, message in
            guard let self else { return }
            DispatchQueue.main.async {
                self.isLoading = false
                if success {
                    self.successMessage = message ?? NSLocalizedString("Пароль успешно обновлен.", comment: "")
                } else {
                    self.errorMessage = message ?? NSLocalizedString("Не удалось обновить пароль.", comment: "")
                }
            }
        }
    }
}
