/// Selected Role State Provider
///
/// Manages the selected user role during registration.
/// Simple state provider for role selection in the registration form.
///
/// Uses Riverpod 3.0 with code generation for type-safe state management.
library;

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:worker/features/auth/domain/entities/user.dart';

part 'selected_role_provider.g.dart';

/// Selected Role Provider
///
/// Manages the currently selected user role in the registration form.
/// Provides methods to select and clear role selection.
///
/// This provider is used to:
/// - Track which role the user has selected
/// - Conditionally show/hide verification fields based on role
/// - Validate required documents for dealer/worker roles
///
/// Usage in widgets:
/// ```dart
/// // Watch the selected role
/// final selectedRole = ref.watch(selectedRoleProvider);
///
/// // Select a role
/// ref.read(selectedRoleProvider.notifier).selectRole(UserRole.customer);
///
/// // Clear selection
/// ref.read(selectedRoleProvider.notifier).clearRole();
///
/// // Show verification section conditionally
/// if (selectedRole == UserRole.customer) {
///   VerificationSection(),
/// }
/// ```
@riverpod
class SelectedRole extends _$SelectedRole {
  /// Initialize with no role selected
  @override
  UserRole? build() {
    return null;
  }

  /// Select a user role
  ///
  /// Updates the state with the newly selected role.
  /// This triggers UI updates that depend on role selection.
  ///
  /// Parameters:
  /// - [role]: The user role to select
  ///
  /// Example:
  /// ```dart
  /// // User selects "Đại lý hệ thống" (dealer)
  /// ref.read(selectedRoleProvider.notifier).selectRole(UserRole.customer);
  /// // This will show verification fields
  /// ```
  void selectRole(UserRole role) {
    state = role;
  }

  /// Clear the role selection
  ///
  /// Resets the state to null (no role selected).
  /// Useful when resetting the form or canceling registration.
  ///
  /// Example:
  /// ```dart
  /// // User clicks "Cancel" or goes back
  /// ref.read(selectedRoleProvider.notifier).clearRole();
  /// // This will hide verification fields
  /// ```
  void clearRole() {
    state = null;
  }

  /// Check if a role is currently selected
  ///
  /// Returns true if any role has been selected, false otherwise.
  bool get hasSelection => state != null;

  /// Check if verification is required for current role
  ///
  /// Returns true if the selected role requires verification documents
  /// (CCCD, certificates, etc.). Currently only customer role requires this.
  ///
  /// This is used to conditionally show the verification section:
  /// ```dart
  /// if (ref.read(selectedRoleProvider.notifier).requiresVerification) {
  ///   // Show CCCD input, file uploads, etc.
  /// }
  /// ```
  bool get requiresVerification => state == UserRole.customer;

  /// Get the display name for the current role (Vietnamese)
  ///
  /// Returns a user-friendly Vietnamese name for the selected role,
  /// or null if no role is selected.
  ///
  /// Example:
  /// ```dart
  /// final displayName = ref.read(selectedRoleProvider.notifier).displayName;
  /// // Returns: "Đại lý hệ thống" for customer role
  /// ```
  String? get displayName {
    if (state == null) return null;

    switch (state!) {
      case UserRole.customer:
        return 'Đại lý/Thầu thợ/Kiến trúc sư';
      case UserRole.sales:
        return 'Nhân viên kinh doanh';
      case UserRole.admin:
        return 'Quản trị viên';
      case UserRole.accountant:
        return 'Kế toán';
      case UserRole.designer:
        return 'Thiết kế';
    }
  }
}

/// Convenience provider for checking if verification is required
///
/// Returns true if the currently selected role requires verification
/// documents (CCCD, certificates, etc.).
///
/// Usage:
/// ```dart
/// final needsVerification = ref.watch(requiresVerificationProvider);
/// if (needsVerification) {
///   // Show verification section with file uploads
/// }
/// ```
@riverpod
bool requiresVerification(Ref ref) {
  final selectedRole = ref.watch(selectedRoleProvider);
  return selectedRole == UserRole.customer;
}

/// Convenience provider for getting role display name
///
/// Returns a user-friendly Vietnamese name for the selected role,
/// or null if no role is selected.
///
/// Usage:
/// ```dart
/// final roleName = ref.watch(roleDisplayNameProvider);
/// if (roleName != null) {
///   Text('Bạn đang đăng ký với vai trò: $roleName');
/// }
/// ```
@riverpod
String? roleDisplayName(Ref ref) {
  final selectedRole = ref.watch(selectedRoleProvider);

  if (selectedRole == null) return null;

  switch (selectedRole) {
    case UserRole.customer:
      return 'Đại lý/Thầu thợ/Kiến trúc sư';
    case UserRole.sales:
      return 'Nhân viên kinh doanh';
    case UserRole.admin:
      return 'Quản trị viên';
    case UserRole.accountant:
      return 'Kế toán';
    case UserRole.designer:
      return 'Thiết kế';
  }
}
