import 'package:flutter/material.dart';
import 'package:get/get.dart';

abstract class BaseProfileController extends GetxController {
  // Common state variables
  final RxBool isLoading = false.obs;
  final RxString errorMessage = ''.obs;
  final RxBool hasChanges = false.obs;

  // Form key for validation
  final formKey = GlobalKey<FormState>();

  // Loading state management
  void setLoading(bool loading) {
    isLoading.value = loading;
  }

  // Error handling
  void setError(String message) {
    errorMessage.value = message;
  }

  // Clear error
  void clearError() {
    errorMessage.value = '';
  }

  // Show error dialog
  void showError(String title, String message) {
    Get.snackbar(
      title,
      message,
      snackPosition: SnackPosition.BOTTOM,
      duration: const Duration(seconds: 4),
      backgroundColor: Colors.red.withOpacity(0.8),
      colorText: Colors.white,
    );
  }

  // Show success message
  void showSuccess(String title, String message) {
    Get.snackbar(
      title,
      message,
      snackPosition: SnackPosition.BOTTOM,
      duration: const Duration(seconds: 2),
      backgroundColor: Colors.green.withOpacity(0.8),
      colorText: Colors.white,
    );
  }

  // Common validation methods
  String? validateRequiredField(String? value, String fieldName) {
    if (value == null || value.isEmpty) {
      return '$fieldName is required';
    }
    return null;
  }

  String? validateEmail(String? value) {
    if (value == null || value.isEmpty) {
      return 'Email is required';
    }
    if (!GetUtils.isEmail(value)) {
      return 'Enter a valid email address';
    }
    return null;
  }

  String? validatePhone(String? value) {
    if (value == null || value.isEmpty) {
      return 'Phone number is required';
    }
    if (!GetUtils.isPhoneNumber(value)) {
      return 'Enter a valid phone number';
    }
    return null;
  }

  String? validateName(String? value) {
    if (value == null || value.isEmpty) {
      return 'Name is required';
    }
    if (value.length < 2) {
      return 'Name is too short';
    }
    return null;
  }

  // Abstract methods to be implemented by subclasses
  Future<bool> saveChanges();
  void discardChanges();
  Future<void> loadData();
}
